温习
温故知新
1. 路由分发
方式一:include
from django.urls import path, include
urlpatterns = [
path('app/', include('app.urls'))
]
方式二:手工分发
urlpatterns = [
# path('user/login/', login, name='login'),
# path('user/logout', logout, name='logout')
path('user/', ([
path('login/', login, name='login'),
path('logout/', logout, name='logout')
], None, None))
]
手工分发纯粹帮忙提取功能的URL,防止重写,接触的项目多了,这个方式会遇到
2. name
2.1 无参数的反向解析
urlpatterns = [
path('user/login/', login, name='login'),
path('user/auth/', index, name='auth')
]
在路由中,可以添加 name, 有了 name 后,可以用到视图函数或者HTML模版中
- 视图View
from django.urls import reverse
from django.shortcuts import redirect
url = reverse('login') # /user/login/
url = reverse('auth') # /user/auth/
- HTML模版
<a href="{% url 'auth' %}">跳转</a>
模版中, url 通过反向解析 name,得出 url 放到 html 页面当中
2.2 带参数的反向解析
urlpatterns = [
path('login/<str:role>/', login, name='login'),
re_path('auth/(\d+)/(\w+)/', index, name='auth'),
re_path('home/(?P<nid>\d+)/(?P<name>\w+)/', home, name='home')
]
- 视图View
from django.urls import reverse
from django.shortcuts import redirect
url = reverse('login', kwargs={'role':'man'}) # /login/man/
url = reverse('auth', args=(123, 'auth') # /auth/123/auth/
url = reverse('home', kwargs={'nid': 123, 'name':'aqiang'}) # /home/123/aqiang/
- HTML模版
<a href="{% url 'auth' 123 'auth' %}">跳转</a>
<a href="{% url 'login' role='aqiang' %}">跳转</a>
3. namespace
用来辅助 name 的作用
-
主路由
from django.urls import path, re_path, include urlpatterns = [ path('api/', include("apps.api.urls",namespace='x1')), path('web/', include("apps.web.urls",namespace='x2')), ]
-
api/urls
from django.urls import path, re_path from . import views urlpatterns = [ path('login/', views.login,name="login"), path('auth/', views.auth, name='auth'), ]
-
web/urls
from django.urls import path, re_path from . import views urlpatterns = [ path('home/', views.home,name='home'), path('order/', views.order,name='order'), path('auth/', views.order, name='auth'), ]
以后在不同的组件中若有相同的 name,可以通过 namespace 确定是哪一个路由
from django.urls import reverse
url = reverse("x1:login") # /api/login/
url = reverse("x1:order") # /web/login/
url = reverse("x1:auth") # /api/login/
url = reverse("x2:auth") # /web/login/
4. request
from django.shortcuts import HttpResponse
def login(request):
# 1.当前URL /api/login/
print(request.path_info)
# 2.URL传递的参数
print(request.GET)
print(request.GET.get("age"))
# 3.请求方式 GET/POST
print(request.method)
# 4.如果post请求,传递请求体(原始数据)
print(
request.body) # b'{"code":"083Sjmll2yla694F3bll2DguCM2SjmlG","unionId":"oP6QCsyT_9bk1dfSaVf0GEV5Y-yE"}' b'v1=123&v2=456'
# 4.1 请求体+请求头 b'v1=123&v2=456' + content-type:application/x-www-form-urlencoded
print(request.POST)
print(request.POST.get("v1"))
print(request.POST.get("v2"))
# 4.2 请求体+请求头 文件
print(request.FILES) # 文件格式 + multipart/form-data
print(request.FILES.get("n1"))
print(request.FILES.get("n2"))
# 5.请求头
# {'Content-Length': '', 'Content-Type': 'text/plain', 'Host': '127.0.0.1:8000', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Sec-Ch-Ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"', 'Sec-Ch-Ua-Mobile': '?0', 'Sec-Ch-Ua-Platform': '"macOS"', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Sec-Fetch-Site': 'none', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-User': '?1', 'Sec-Fetch-Dest': 'document', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,zh-TW;q=0.7', 'Cookie': 'csrftoken=CdidpKSGbLxzmOXnbmlkvrZep1eJmKLAA81T73UjcjxEnMOa4YOZqtc849AkYfUy'}
print(request.headers)
# 5.1 请求头有个特殊的cookie
# request.headers['cookie'] # 'csrftoken=CdidpKSGbLxzmOXnbmlkvrZep1eJmKLAA81T73UjcjxEnMOa4YOZqtc849AkYfUy;session=xxxx'
# {'csrftoken': 'CdidpKSGbLxzmOXnbmlkvrZep1eJmKLAA81T73UjcjxEnMOa4YOZqtc849AkYfUy'}
print(request.COOKIES)
# 6.requests中其他值
print(request.resolver_match)
return HttpResponse("login")
5. 静态文件
settings.py
STATIC_URL = '/static/'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
-
顺序:…
-
多app开发:各自app的图片放在各自 /static/app名字/…
-
在开发过程中
- 禁止
<img src="/static/api/1.png">
- 建议
{% load static %} ... <img src="{% static 'api/1.png' %}">