django中request的POST小记

Posted gaota1996

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>

 

以上是关于django中request的POST小记的主要内容,如果未能解决你的问题,请参考以下文章

GET和POST小记

django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE的解决办法(转)(代码片段

request.POST和django表单中的字典数据有什么区别?

Django:如何在删除视图中使用 request.POST

Django 错误的 request.POST 属性值已保存

数据访问:Django 表单与 request.POST 方法 [关闭]