-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdjango_base.txt
138 lines (99 loc) · 3.6 KB
/
django_base.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
服务器程序和应用程序
WSGI
socket封装
wsgiref
MVC:Model View Controller
model 数据库相关
view html页面
controller 处理请求
MTV:Model Template View
template html页面
view 处理请求
路由系统
class test:
def __str__(self):
return 'this is in str'
st = test()
print st
# __str__ 直接打印对象的实现方法
models 中的类相当于数据库中的 table
models 中的每个属性是数据库中的一个字段(一个列)
models.py
from django.db import models
class Reporter(models.Model):
full_name = models.CharField(max_length=70)
def __str__(self): # __unicode__ on Python 2
return self.full_name
class Article(models.Model):
pub_date = models.DateField()
headline = models.CharField(max_length=200)
content = models.TextField()
reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
def __str__(self): # __unicode__ on Python 2
return self.headline
admin.py
from django.contrib import admin
from . import models
admin.site.register(models.Article)
# 注册模型动态生成管理页面
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/([0-9]{4})/$', views.year_archive),
url(r'^articles/([0-9]{4})/([0-9],{2})/$', views.month_archive),
url(r'^admin/', admin.site.urls),
]
# urlconf 如果请求url为 /articles/2005/05/ 则调用函数 news.views.month_archive(request, '2005', '05')
views.py
from django.shortcuts import render
from .models import Article
def year_archive(request, year):
a_list = Article.objects.filter(pub_date_year=year)
context = {'year': year, 'article_list': a_list}
return render(request, 'news/year_archive.html', context)
# 每个视图只负责返回 httpresponse 对象或者抛出一个异常。
url()
regex view kwargs name
settings.py
INSTALLED_APPS = [
'django.contrib.admin', # 管理站点
'django.contrib.auth', # 认证系统
'django.contrib.contenttypes', # 用于内容类型的框架
'django.contrib.sessions', # 会话框架
'django.contrib.messages', # 消息框架
'django.contrib.staticfiles', # 管理静态文件的框架
'app1.apps.App1Config', # 新建的app
]
# Django 会从 MIDDLEWARE_CLASSES 中按照从上到下的顺序一个个执行中间件中的 process_request 函数,而其中 process_response 函数则是最前面的最后执行。
# process_request()
# process_exception() (only if the view raised an exception)
# process_template_response()
# process_response()
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',
]
migration 迁移
python manage.py makemigrations # app1/migrations/0001_initial.py 生成迁移文件
python manage.py sqlmigrate app1 0001 # 查看迁移文件的SQL语句
python manage.py migrate # 将改变更新到数据库中
def __str__():
return self.question_text
imporrt django
django.setup()
# 效果相当于 python manage.py shell
# 需要先设置环境变量 DJANGO_SETTINGS_MODULE=dj1.settings,
# 需要确保 python path 可以运行 import dj1
app1/admin.py
from django.contrib import admin
from .models imporrt Question
admin.site.registerr(Question)
127.0.0.1:8000/admin
python manage.py runserver 0.0.0.0:8000
settings.py ALLOWED_HOSTS=['*']