Table of contents
( 3) POST form data acquisition and processing
(4) Handling of login failures
(5) Handling of successful login
1. Create a template
views.py
def something(request):
#request是一个对象,封装了用户发送来的所有请求相关的数据
#1、获取请求方式
#2、在url上面传递值http://127.0.0.1:8000/something/?n1=123&n2=789
print(request.GET)
#3、在请求体中提交数据
return HttpResponse("返回内容")
urls.py
path('something/',views.something),
access:
output:
2. Three request methods
#1、获取请求方式
print(request.method)
#2、在url上面传递值http://127.0.0.1:8000/something/?n1=123&n2=789
print(request.GET)
#3、在请求体中提交数据
print(request.POST)
3. Three response methods
#4、【响应】HttpResponse("返回内容"),将字符串返回给请求者
#return HttpResponse(123)
#5、【响应】读取HTML的内容+渲染(替换)->字符串(网页源码),返回给用户浏览器
#return render(request,'something.html',{"title":"requst"})
#6、【响应】让浏览器重定向
return redirect("https://www.baidu.com/")
Redirect processing:
4. Case: fake user login
(1) Create a template
views.py
import json
from django.shortcuts import render,HttpResponse,redirect
# Create your views here.
def index(request):
return HttpResponse("Hello Django!")
def user_list(requets):
return render(requets,'user_list.html')
def user_add(requets):
return render(requets,'user_add.html')
def tpl(requets):
name="代码骑士"
name_list=["小明","小红","李华","康康"]
role_dicts={"name":"小明","salary":100000,"position":"CEO"}
data_list=[
{"name": "小明", "salary": 100000, "position": "CEO"},
{"name": "小红", "salary": 100000, "position": "HR"},
{"name": "康康", "salary": 100000, "position": "CTO"}
]
return render(requets,'tpl.html',{"n1":name,"n2":name_list,"n3":role_dicts,"n4":data_list})
def news(requet):
#定义一个列表或字典存储数据
#向网址:http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/09/news发送请求
#使用第三方模块:requests
import requests
url = "http://www.chinaunicom.com.cn/api/article/NewsByIndex/2/2022/09/news"
headers = {'User-Agent': 'Mozilla/4.0'}
res = requests.get(url,headers=headers)
data_list=res.json()
print(data_list)
return render(requet,'news.html',{"news_list":data_list})
def something(request):
#request是一个对象,封装了用户发送来的所有请求相关的数据
#1、获取请求方式
print(request.method)
#2、在url上面传递值http://127.0.0.1:8000/something/?n1=123&n2=789
print(request.GET)
#3、在请求体中提交数据
print(request.POST)
#4、【响应】HttpResponse("返回内容"),将字符串返回给请求者
#return HttpResponse(123)
#5、【响应】读取HTML的内容+渲染(替换)->字符串(网页源码),返回给用户浏览器
#return render(request,'something.html',{"title":"requst"})
#6、【响应】让浏览器重定向
return redirect("https://www.baidu.com/")
def login(request):
if request.method == "GET":
return render(request,'login.html')
else:
#如果是POST请求,获取用户提交的数据
print(request.POST)
return HttpResponse("登录成功!")
urls.py
from django.urls import path
from app import views
urlpatterns = [
path('index/', views.index),
path('user/list/',views.user_list),
path('user/add/',views.user_add),
path('tpl/',views.tpl),
path('news/',views.news),
path('something/',views.something),
#用户登录
path('login/',views.login),
]
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post" action="/login/">
<input type="text" name="user" placeholder="用户名">
<input type="password" name="pwd" placeholder="密码">
<input type="submit" value="提交"/>
</form>
</body>
</html>
access:
It seems that there is no problem, but click submit
This is because, when making a post request, Django has another layer of encryption protection mechanism, which cannot directly access the post form, and needs to make special instructions in html:
(2) csrf_token verification
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post" action="/login/">
{% csrf_token %}
<input type="text" name="user" placeholder="用户名">
<input type="password" name="pwd" placeholder="密码">
<input type="submit" value="提交"/>
</form>
</body>
</html>
Just submit it again
Django will automatically generate a hidden code for token encryption, which is used for form verification and enhances confidentiality.
Do not add this code (note that this code must be added to the form form.):
{% csrf_token %}
Django can't verify the submission form, and can't find the jump interface.
(3) POST form data acquisition and processing
def login(request):
if request.method == "GET":
return render(request,'login.html')
else:
#如果是POST请求,获取用户提交的数据
print(request.POST)
username=request.POST.get("user")
password = request.POST.get("pwd")
if username == 'root' and password == '123456':
return HttpResponse("登录成功!")
else:
return HttpResponse("登录失败!")
(4) Handling of login failures
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post" action="/login/">
{% csrf_token %}
<input type="text" name="user" placeholder="用户名">
<input type="password" name="pwd" placeholder="密码">
<input type="submit" value="提交"/>
<span style="color: red">{
{ error_msg }}</span>
</form>
</body>
</html>
access:
(5) Handling of successful login
def login(request):
if request.method == "GET":
return render(request,'login.html')
#如果是POST请求,获取用户提交的数据
print(request.POST)
username=request.POST.get("user")
password = request.POST.get("pwd")
if username == 'root' and password == '123456':
#return HttpResponse("登录成功!")
return redirect("https://blog.csdn.net/qq_51701007?spm=1000.2115.3001.5343")
#return HttpResponse("登录失败!")
return render(request,'login.html',{"error_msg":"用户名或密码错误!"})
Finish uploading gitee.