1.请求与响应
def test(request):
# 获取请求方法
print(request.method)
# 获取get的url请求参数
print(request.GET)
# 获取post请求体参数
print(request.POST)
# 内容字符串形式返回
# return HttpResponse("返回内容")
# 使用render重新渲染,渲染后返回
# return render(request,"test.html",{"title":"abc"})
# 重定向
return redirect("https://www.baidu.com")
2.一个案例
def login(req):
if req.method == "GET":
return render(req, "login.html")
print(req.POST)
username = req.POST.get("user")
password = req.POST.get("pwd")
if username == "admin" and password == "123":
return redirect("https://www.baidu.com")
return render(req, "login.html", {"error_msg": "用户名或密码错误"})
<h1>用户登录</h1>
<form method="post" action="/login/">
{% csrf_token %}
<input type="text" name="user" placeholder="用户名"/><br/>
<input type="password" name="pwd" placeholder="密码"/>
<input type="submit" value="提交"/>
<span style="color: red;">{{ error_msg }}</span>
</form>
{% csrf_token %}具体可了解:Django中CSRF原理及应用详解
标签:return,render,req,request,笔记,django,响应,print,POST From: https://www.cnblogs.com/CarlosX/p/16950036.html