
setting.py中:
"""
Django settings for untitled3 project. Generated by 'django-admin startproject' using Django 2.0.7. For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/ For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
""" import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '3p4)ob=u_tpk_ha+5fs1x8vfn+(s5-92$(05%r04ny9v+dv=qp' # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01',
] MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
] ROOT_URLCONF = 'untitled3.urls' TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')]
,
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
] WSGI_APPLICATION = 'untitled3.wsgi.application' # Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
} # Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
] # Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/ LANGUAGE_CODE = 'en-us' TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/ STATIC_URL = '/static/'
models.py中(数据库创建表结构都在这里):
from django.db import models # Create your models here.
class Business(models.Model):
caption = models.CharField(max_length=32)
code = models.CharField(max_length=32,null=True,default='SA')#default是设置默认值,null=True是数据库中允许这个字段为空 class Host(models.Model):
nid = models.AutoField(primary_key=True) #设置主键并自增 hostname = models.CharField(max_length=32,db_index=True)#max_length设置字段最大长度 ip = models.GenericIPAddressField(protocol='ipv4',db_index=True)#db_index=True设置为索引 port = models.IntegerField() b = models.ForeignKey(to="Business", to_field='id',on_delete=models.CASCADE)#报错加上了on_delete=models.CASCADE1
#设置外键关联Business中的id字段也可写成b = models.ForeignKey('Business',to_field='id')
urls.py中(路由都在这里):
"""untitled3 URL Configuration The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from app01 import views
from django.conf.urls import url urlpatterns = [
path('admin/', admin.site.urls),
url(r'^business/$',views.business),#$是结束符
]
views.py中(主要业务逻辑都在这里):
from django.shortcuts import render
from app01 import models
# Create your views here.
def business(request): v1 = models.Business.objects.all() #QuerySet
#[obj(id,caption,code),obj(id,caption,code),obj(id,caption,code)] 里面的小元素是对象 v2 = models.Business.objects.values('id', 'caption') # 拿固定列
# QuerySet
# [{'id':1,'caption':'xx','code':'da'},{....},{.....}] 里面的小元素是字典 v3 = models.Business.objects.values_list('id', 'code')
# QuerySet
# [(1,开发),(2,运维)] 里面的小元素是字典 return render(request,'business.html',{'v1':v1,'v2':v2,'v3':v3}) #注意:一定要加return 第一个参数request是对象不是字符串!不是字符串!不是字符串!
templates下的XXXX.html中(网页模板都放templates下):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <h1> 对象 </h1>
<ul>
{% for row in v1 %} <li> {{ row.caption }}-{{ row.code }} </li> {% endfor %}
</ul> <h1> 字典 </h1> <ul>
{% for row in v2 %} <li> {{ row.id }}-{{ row.caption }} </li> {% endfor %}
</ul> <h1> 元组 </h1> <ul>
{% for row in v3 %} <li> {{ row.0}}-{{ row.1 }} </li> {% endfor %}
</ul> </body>
</html>