A, request objects common attributes:
Two, QueryDict objects:
- Package is located: django.http.QueryDict
HttpRequest
ObjectsGET
andPOST
attributes areQueryDict
type- With different python dictionary: QueryDict a key target can hold multiple values
get()
Method: Get value according to the key, if a key and has a plurality of values, the last value acquired; None key does not exist, if the return value may be set to return a custom default valuegetlist()
Method: obtaining a plurality of values according to the key, it returns a list of values; empty list if the key is not present is returned []- Demo:
Third, write code:
- views view function
-
from django.shortcuts import render,redirect from django.http import HttpResponse # Create your views here. def index(request): return HttpResponse('登录成功') def login(request): return render(request, 'mylogin/login.html') def login_check(request): # 1.获取提交的用户名和密码 username = request.POST.get('username') # 表单的name是什么 这里就传什么 password = request.POST.get('password') # 2.校验并返回应答 if username == 'wen' and password == '123': # 注意这里写字符串 123要加引号 return redirect('/index') else: return redirect('/login')
- Template file code
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <h1>注册表单</h1> <form method='POST' action='/login_check'> <p> <label for="">用户名:</label> <input type="text" name="username"> </p> <p> <label for="">密 码:</label> <input type="password" name="password"> </p> <p> <input type="submit" value="登录"> </p> </form> </body> </html>
-
Note: Django Dui
POST
,PUT
,PATCH
,DELETE
requests to open the way CSRF security, to facilitate the test, you cansettings.py
comment out the CSRF middleware file, closeCSRF
protection
Fourth, case presentations:
- login successful:
- Login failed:
V. Summary: This small case mainly understand GET and POST attributes attribute request request.