Django-47-request之POST提交数据

论坛 期权论坛 脚本     
匿名技术用户   2020-12-30 09:34   32   0

前言

post请求主要用于向服务器提交数据,而提交的数据不会以明文的形式展现在url上。我们通常用post做注册,登录等向服务器提交数据的功能。

通常进行Post的方式:form表单、Ajax请求、接口

基于上一篇文章中设置好的路由以及视图、html来修改

本篇post请求基于form表单方式

django_study.templates.test_request.html 页面显示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test Request</title>
</head>
<body>
    <form action="" method="post" name="reg_form">
        {% csrf_token %}
        账号:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        <input type="submit" name="提交">
    </form>
</body>
</html>

django_study.app01.views.py.test_request 视图函数

def test_request(request):
    if request.method == "POST" and request.POST: # 如果请求是post方式且有数据
        username = request.POST["username"]
        password = request.POST["password"]
        return HttpResponse("输入的用户名为:%s,密码为:%s"%(username, password))
    return render(request, "test_request.html", locals())

django_study.django_study.urls.py路由

url(r'^test_request/$', test_request)

运行状态访问:127.0.0.1:8000/test_request/

输入账号密码后提交

控制台可以看到(访问路径get请求,提交数据post请求)

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP