客户端给服务器传递数据所用
path 请求的完整路径,不包含域名和端口
method 请求方式,常用的get和post
encoding 提交数据的编码格式
值:如果为None,表示使用浏览器默认的编码格式,一般为utf-8
GET 一个类似字典的对象,包含get请求方式的所有参数
POST 一个类似字典的对象,包含post请求方式的所有参数
如果表单提交失败 查看是否存在{% csrf_token %} 或者将Middleware中的csrf关闭
FILES 一个类似字典的对象,包含所有的上传文件
COOKIES 是一个标准的字典,包含了所有的cookie
session 一个类似字典的对象,表示当前会话信息,只有Django开启session服务后才会有该属性,默认是开启的
META 包含了http请求的所有header头信息
格式化后显示
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">requestAttr</span><span class="hljs-params">(request)</span>:</span>
values = request.META.items()
html = []
<span class="hljs-keyword">for</span> k,v <span class="hljs-keyword">in</span> values:
html.append(<span class="hljs-string">'<tr><td>{}</td><td>{}</td></tr>'</span>.format(k,v))
<span class="hljs-comment"># return HttpResponse('request属性')</span>
<span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">'<table>%s</table>'</span>%<span class="hljs-string">'\n'</span>.join(html))
常用的key值
示例
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">index</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-comment">#分析客户想干什么(客户给服务的数据)</span>
print(request.path)
print(request.method)
print(request.encoding)
print(request.GET)
print(request.POST)
print(request.FILES)
print(request.COOKIES)
print(request.session)
<span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"lucky is a good man"</span>)
格式
request.GET
URL地址为
http://127.0.0.1:8000/lucky/?a=1&b=2&c=3
示例
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get1</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">"GET"</span>:
<span class="hljs-comment">#获取客户端传递的数据</span>
a = request.GET.get(<span class="hljs-string">"a"</span>)
b = request.GET.get(<span class="hljs-string">"b"</span>)
c = request.GET.get(<span class="hljs-string">"c"</span>)
<span class="hljs-keyword">return</span> HttpResponse(a+b+c)
URL地址为
http://127.0.0.1:8000/lucky/?a=1&a=2&c=3
示例
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">get2</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">"GET"</span>:
alist = request.GET.getlist(<span class="hljs-string">"a"</span>)
c = request.GET.get(<span class="hljs-string">"c"</span>)
<span class="hljs-keyword">return</span> HttpResponse(alist[<span class="hljs-number">0</span>]+alist[<span class="hljs-number">1</span>]+c)
注册界面 register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<form action="/register/" method="post">
{% csrf_token %}
账号:<input type="text" name="account"><br/>
密码:<input type="password" name="password"><br/>
验证密码:<input type="password" name="yzm"><br/>
<input type="submit" value="注册">
</form>
</body>
</html>
路由
re_path(<span class="hljs-string">r'register/'</span>, views.register),
视图
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">register</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-keyword">if</span> request.method == <span class="hljs-string">"GET"</span>:
<span class="hljs-keyword">return</span> render(request, <span class="hljs-string">"App/register.html"</span>)
<span class="hljs-keyword">else</span>:
<span class="hljs-comment">#获取客户端传递的数据</span>
account = request.POST.get(<span class="hljs-string">"account"</span>)
password = request.POST.get(<span class="hljs-string">"password"</span>)
yzm = request.POST.getlist(<span class="hljs-string">"yzm"</span>)
print(account)
print(password)
print(yzm)
<span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">"注册成功"</span>)