创建project
https://www.runoob.com/django/django-first-app.html
# 创建project
django-admin startproject diango1
#创建app
cd .\diango1\
django-admin startapp app1
#运行
python manage.py runserver 0.0.0.0:8000
创建app
在app1 下新建 urls 文件 \diango1\app1\urls.py
from django.contrib import admin
from django.urls import path
from app1.views import hello
urlpatterns = [
path('app1/', hello),
]
修改 app1/views.py 的内容
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world ! ")
修改 diango1/urls.py 添加path('app1/', include('app1.urls')),
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('app1/', include('app1.urls')),
]
修改 diango1/settings.py
# INSTALLED_APPS=[] 数组内添加 'app1'
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1'
]
浏览器输入
http://127.0.0.1:8000/app1/app1/
即可看到hello world
Django administration
创建项目时在其urlpatterns 已包含admin
urlpatterns = [
path('admin/', admin.site.urls),
]
默认使用sqllite3配置 可查看 diango1/settings.py 中DATABASES 配置
python manage.py makemigrations
python manage.py migrate #sqlite表中生成 表结构 db.sqlite3
python manage.py createsuperuser #创建个用户
浏览器输入
http://127.0.0.1:8000/admin
将普通model由admin管理
from django.db import models
# Create your models here. app1/models.py
class CodeModel(models.Model):
name = models.CharField(max_length=50) # 名字最长为 50 个字符
code = models.TextField() # 这个字段没有文本长度的限制
def __str__(self):
return 'Code(name={},id={})'.format(self.name,self.id)
from django.contrib import admin
# Register your models here. app1/admin.py
from django.contrib import admin
from .models import CodeModel
@admin.register(CodeModel) #重要添加的 交由admin
class CodeModelAdmin(admin.ModelAdmin):
pass
中间键
from django.utils.deprecation import MiddlewareMixin
# from django.http import HttpResponse
from django.shortcuts import HttpResponse, redirect
# 方式一:
class MyMiddleware(MiddlewareMixin):
def process_request(self, request):
print("process_request",request)
pass
def process_view(self, request, view_func, view_args, view_kwargs):
print("process_view")
pass
def process_exception(self, request, exception):
pass
def process_response(self, request, response):
print("process_request",request,response)
return response
注册 diango1/settings.py
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',
'app1.middlewares.MyMiddleware'
]
2.1 init方法要调用父类方法
init方法在启动服务的时候执行一次
2.2 process_request(self,request)
请求进入中间件后,第一个执行的方法
返回值有两个:Response|None
返回Response,不会再执行视图函数,而是调到process_response方法(此处有坑,最后说)
返回None,或者不return,继续运行
2.3 process_view(self,request,func,args,*kwargs)
运行完process_request后,就运行这个
func是要执行的视图函数,所以:response = func(request) return response,可以得到视图函数的结果
如果调用了func(request)方法,如果不return response的话,视图函数还会执行一次,所以要不就不调用,调用就return
如果手动调用了func(request)方法,视图函数中的异常不会被process_exception(self,request,exception)接收
2.4 process_response(self,request,response)
每次返回response的时候必经的方法
必须return 一个 response,否则页面显示啥?