python学习笔记(11)Django路由和传值
1. 路由相关
from django.contrib import admin from django.urls import path from django.urls.conf import include from django.urls import re_path # 正则表达式的路径 from app01 import views urlpatterns = [ path('admin/', admin.site.urls), path("timer/", views.get_timer), path("login/", views.login), path("auth", views.auth), path("",views.index), # 路由系统 # 路径参数传参 re_path("article/(\d{4})/(\d+)",views.article_detail), # 分组,一个()一个分组,这边2个()是2个分组,在article_detail需要加2个参数def article_detail(request,year,month):。 # 有名分组命名方式就是?P<参数>注意是大写P re_path("article_achive/(?P<year>\d{4})/(?P<month>\d+)",views.article_achive), # 上面是位置传参,可以给参数命名?P<month>方便使用,def article_achive(request,year="2012",month="12") # 路由分发 path("app01/", include("app01.urls")), path("app02/", include("app02.urls")), ]
2. 数据获取
def index(request): # 获取请求方式 print(request.method) # 获取请求路径不带参数 print(request.path) # 获取完整的请求路径 print(request.get_full_path()) # 请求数据 print("GET:::",request.GET) # GET::: <QueryDict: {'a': ['1'], 'pwd': ['321']}> print("GET:::",request.GET.get("pwd")) # GET::: 321 print("POST:::",request.POST) # POST::: <QueryDict: {'USER': ['YUAN'], 'PASSWORD': ['3213213213'], 'hobor': ['A', 'B']}> print("POST:::",request.POST.get("hobor")) # POST::: B print("POST_list:::",request.POST.getlist("hobor")) # POST::: ['A', 'B'] # 使用json传值的时候 print("BODY:::",request.body) # BODY::: b'{"user":"yuan","pwd":"wwwwww"}' # 使用Json反序列化得到字典 data = json.loads(request.body.decode()) print(data) # {'user': 'yuan', 'pwd': 'wwwwww'} # 请求头 print("请求头:",request.META)
posted on 2021-08-21 15:51 torotoise512 阅读(131) 评论(0) 收藏 举报
浙公网安备 33010602011771号