路由能够定义在工程的目录下(看你的需求),也能够定义在各个应用中来保存应用的路由,用主路文件urls中使用include()包含各个应用的子路由的数据python
Django接收到请求后,从主路由文件urlpatterns中的路由从上倒下顺序查找,若是有include包含,则进入子应用的urls中的urlpatterns中查找(从上而下)django
Django有/结尾路由,用户不须要加/,就能够直接重定向到/结尾的路径上json
如:服务器
# 主路由
from django.conf.urls import url,include from django.contrib import admin import django_test.urls urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^users/',include(django_test.urls ,namespace='users')) ]
注意点:函数
from django.shortcuts import render # Create your views here. from django.http import HttpResponse from django.urls import reverse def index(request): return HttpResponse('欢迎来到Gaidy博客') def show(request): url = reverse('users:index') print(url) return HttpResponse('OK')
post请求那些须要到postman测试工具里面测试工具
先把CSRF防御注释掉post
URL:直接在URL中传递数据测试
查询字符串:key1=value1&key2=value2;url
请求体:在body中传递数据,常见有表单,json,xmlspa
请求头:在http报文头中
子应用的路由设置
urlpatterns = [ # 这边定义子应用的路由 url(r'^index/$',views.index,name='index'), url(r'^show/$',views.show,name='show'), url(r'^parameter/([a-z]+)/(\d{4})$',views.parameter,name='parameter'), ]
定义视图函数
# name,和age参数位置调换会影响下面的输出结果 def parameter(request,name, age): print('age=%s'%age) print('name=%s' % name) return HttpResponse('OK')
命名参数按照名字传递
子路由
urlpatterns = [ # 这边定义子应用的路由 url(r'^index/$',views.index,name='index'), url(r'^show/$',views.show,name='show'), url(r'^parameter/(?P<name>[a-z]+)/(?P<age>\d{4})$',views.parameter,name='parameter'), ]
视图函数
# age 和name位置改变值不变 def parameter(request,age, name): print('age=%s'%age) print('name=%s' % name) return HttpResponse('OK')
注意:查询字符串不区分请求方式,即假使客户端进行POST方式的请求,依然能够经过request.GET获取请求中的查询字符串数据。
子路由
url(r'^qust/$',views.qust),
视图函数
def qust(request): a = request.GET.get('a') b = request.GET.get('b') alist = request.GET.getlist('a') print(a) # 3 print(b) # 2 print(alist) # ['1', '3'] return HttpResponse('OK')
运行(后面在加)
路由设置
url(r'^get_form/$', views.get_form)
视图函数
def get_form(request): name = request.POST.get('name') age = request.POST.get('age') alist = request.POST.getlist('name') print(name) print(age) print(alist) return HttpResponse('OK')
运行
路由
url(r'^get_body_json/$', views.get_body_json),
视图
def get_body_json(request): json_str = request.body json_str = json_str.decode() # python3.6 无需执行此步 req_data = json.loads(json_str) print(req_data['a']) print(req_data['b']) return HttpResponse('OK')
运行
能够经过request.META属性获取请求头headers的数据
路由
url(r'^get_head/$', views.get_head)
视图函数
def get_head(request): print(request.META['CONTENT_TYPE']) return HttpResponse('OK')
运行
CONTENT_LENGTH
– The length of the request body (as a string).
CONTENT_TYPE
– The MIME type of the request body.
HTTP_ACCEPT
– Acceptable content types for the response.
HTTP_ACCEPT_ENCODING
– Acceptable encodings for the response.
HTTP_ACCEPT_LANGUAGE
– Acceptable languages for the response.
HTTP_HOST
– The HTTP Host header sent by the client.
HTTP_REFERER
– The referring page, if any.
HTTP_USER_AGENT
– The client’s user-agent string.
QUERY_STRING
– The query string, as a single (unparsed) string.
REMOTE_ADDR
– The IP address of the client.
REMOTE_HOST
– The hostname of the client.
REMOTE_USER
– The user authenticated by the Web server, if any.
REQUEST_METHOD
– A string such as "GET"
or "POST"
.
SERVER_NAME
– The hostname of the server.
SERVER_PORT
– The port of the server (as a string).
案例# HttpResponse(content=响应体,content_type=响应数据类型,status=状态码)
# content:表示返回的内容 # status_code:返回的HTTP响应状态码 # content_type: 指定返回数据的MIME类型
from django_http import HttpResponse
def index(request): return HttpResponse('欢迎来到Gaidy博客', status=202)
from django.http import JsonResponse def index(request): return JsonResponse({'name': 'gaidy', 'age': '25'})
运行结果
from django.shortcuts import redirect # django_test是路由的空间命名 def show(request): # 重定向 return redirect(reverse('django_test:index'))