首页 > 技术文章 > django中request的POST小记

gaota1996 2019-02-26 14:47 原文

views.py

from django.http import HttpResponse

def postTest1(request):
    return render(request, 'booktest/postTest1.html')


def postTest2(request):
    uname = request.POST['uname']
    upwd = request.POST['upwd']
    ugender = request.POST['ugender']
    uhobby = request.POST.getlist('uhobby')
    context = {'uname': uname, 'upwd': upwd, 'ugender': ugender, 'uhobby': uhobby}
    return render(request, 'booktest/postTest2.html', context)

urls.py

from booktest import views
from django.urls import path

urlpatterns = [
    path('postTest1/', views.postTest1),
    path('postTest2/', views.postTest2),

]

postTest1.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form method="post" action="/booktest/postTest2/">
    用户名:<input type="text" name="uname"><br>
    密码:<input type="text" name="upwd"><br>
    性别:<input type="radio" name="ugender" value="" checked="checked"><input type="radio" name="ugender" value="">女<br>
    爱好:<input type="checkbox" name="uhobby" value="足球">足球
            <input type="checkbox" name="uhobby" value="篮球">篮球
            <input type="checkbox" name="uhobby" value="毛球">毛球<br>
    <input type="submit" value="提交">
<!--post请求中 name属性作为键提交 value属性作为值提交 -->
</form>
</body>
</html>

postTest2.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
用户名:{{ uname }}<br>
密码:{{ upwd }}<br>
性别:{{ ugender }}<br>
爱好:{{ uhobby }}
</body>
</html>

 

推荐阅读