栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Django15——post传参

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Django15——post传参

文章目录
post传参的键和值是怎么来的?
name属性作为键, value作为值。根据键作为值。

任务:实现用户注册,将注册后的信息显示在页面上。
(1)新建postTest.html页面。


    
用户名:
密码:
性别:男 女
爱好:运动 旅游 唱歌 打游戏

表单中控件的name不写,不会被提交。value不写,会被提交。

细微不同的action值,提交后所生成的url地址是不一样的。

action的值提交后生成的url
/study/postTest2http://127.0.0.1:8000/study/postTest2/
study/postTest2http://127.0.0.1:8000/study/postTest1/study/postTest2/

怎样获取提交后的数据呢?
(2)配置路由

path('postTest1/',views.postTest1),

(3)定义视图

def postTest1(request):
    # 返回postTest1.html
    return render(request,"study/postTest1.html")

(4)查看效果

输入数据,单击提交按钮提交后,将DEBUG = True后效果如下:

解决如下:

输入数据重新提交,效果如下:

(5)再次配置路由

path('postTest2/',views.postTest2),

(6)再次编写视图函数

def postTest2(request):
    # 怎么获取post传参方式中传递的参数
    # name = request.POST["uname"] #获取post传参方式中name为uname控件的值
    # pwd = request.POST["upwd"]
    # sex = request.POST["ugender"]
    name = request.POST.get("uname")  # 获取post传参方式中name为uname控件的值
    pwd = request.POST.get("upwd")
    sex = request.POST.get("ugender")
    hobby = request.POST.getlist("uhobby")
    context = {"name": name, "pwd": pwd, "sex": sex, "hobby": hobby}
    return render(request, "study/postTest2.html", context)

(7)编写postTest2.html


用户名:{{ name }} 
密码:{{ pwd }}
性别:{{ sex }}
爱好:{{ hobby }}
{% for h in hobby %} {{ h }}
{% endfor %}

(8)查看效果

单击提交后,效果如下:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/923952.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号