Django中的文件表示的含义
创建新的app名称:在Terminal中运行 python manage.py startapp app02
admin.py 操作数据库的
wapps.py 当前app的配置文件
models.py 映射类的存放位置与数据库对应
tests.py 做单元测试的
views.py 写函数或者类的后台
admin.py Django自带的后台管理相关配置
apps.py
models.py 写类,根据类创建相关数据库表
tests.py 做单元测试的
views.py 业务处理,这个views不一定是一个py文件,可以是一个文件夹
路由分发:在项目名称->urls->urlpatterns下
path('app01/',include('app01.urls')),
path('app02/',include('app02.urls')),
urlpatterns = [
# path('admin/', admin.site.urls),
# path('index/',views.index),
# 包含其他App中的path
path('app01/',include('app01.urls')),
path('app02/',include('app02.urls')),
# path('项目名/',include('项目名.项目名下面存放url的文件'))
# 出错存在的网页
# path('^',default)\
url(r'^',default),
# path('index/',views.index,name)
# 根据名字反生成url
# url(r'^index/(\w+)/',views.index,name='n1')
]
"""untitled1 URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/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.conf.urls import url
from django.urls import path,include
from app01 import views
from django.shortcuts import HttpResponse
def default(request):
return HttpResponse('出错了')
urlpatterns = [
path('app01/', include('app01.urls')),
path('app02/', include('app02.urls')),
url(r'^',default),
# path('admin/', admin.site.urls),
# path('index/',views.index),
# 包含其他App中的path
# path('app01/',include('app01.urls')),
# path('app02/',include('app02.urls')),
# path('项目名/',include('项目名.项目名下面存放url的文件'))
# 出错存在的网页
# path('^',default)\
# url(r'^',default),
# path('index/',views.index,name)
# 根据名字反生成url
# url(r'^index/(\w+)/',views.index,name='n1')
]
路由系统
a、/login/ --> def login 静态路由
http://127.0.0.1:8000/edit/?nid=fff
path('edit/',views.edit),
def edit(request):
request.GET.get('nid')
return HttpResponse('ooo')
b、/add-user/(\d+)正则表达式 --> def add_user(request) 动态路由
http://127.0.0.1:8000/edit/fff
path('edit/(\w+)/',views.edit),
def edit(request,a1)
(\w+)对应a1
path('edit/(\w+)/.html',views.edit),使其有后缀名
按位置放
path('edit/(\w+)/(\w+)/.html',views.edit)
def edit(request,a1,a2)
前一个 (\w+)对应a1,后一个 (\w+)对应a2
这种可以与def edit(request,*arges,**kwarges)
按名称放
path('edit/(?P<a1>\w+)/(?P<a2>\w+)/.html',views.edit)
终止符
path('edit$',views.edit),
伪静态
path('edit/(\w+).html$',view.edit)
# 出错存在的网页
# path('^',default)\
url(r'^',default)