django中request的POST小記


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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM