如何在 Django 视图中获取 POST 请求值?
我正在尝试通过 html/django 制作注册表单,因此我有 3 个输入框供用户输入电子邮件、用户名和密码,然后通过 POST 将它们发送到 /adduser
<form action="/OmniCloud_App/adduser" method="post">
{% csrf_token %}
Email Address: <input type="text" name="email" /></br>
Username: <input type="text" name="username" maxlength=25 /></br>
Password: <input type="password" maxlength=30 /></br>
</br>
<input type="submit" value="Send" /> <input type="reset">
</form>
adducer 创建一个新用户并将其保存到DB:
def adduser(request, email, username, password):
u = User(email=email, username=username, password=password)
u.save()
return render_to_response('adduser.html', {'email':email, 'username':username, 'password':password})
但是当我单击 /signup 上的“提交”时,它抱怨我只给了它 1 个参数,而预期是 3 个参数。我应该如何将电子邮件、用户名和密码字段从 signup.html 传递到用户名函数(位于 /username)?
I'm trying to make a signup form via html/django so I have 3 input boxes for the user to put in the email, username, and password that then sends them via POST to /adduser
<form action="/OmniCloud_App/adduser" method="post">
{% csrf_token %}
Email Address: <input type="text" name="email" /></br>
Username: <input type="text" name="username" maxlength=25 /></br>
Password: <input type="password" maxlength=30 /></br>
</br>
<input type="submit" value="Send" /> <input type="reset">
</form>
adducer creates a new User and saves it to the DB:
def adduser(request, email, username, password):
u = User(email=email, username=username, password=password)
u.save()
return render_to_response('adduser.html', {'email':email, 'username':username, 'password':password})
but when I click submit on /signup, it complains that I am only giving it 1 parameter when 3 were expected. How should I pass the email,username, and password fields from signup.html to the username function (located at /username)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您阅读本教程的第 3 部分,您会看到该视图函数需要 URL 本身的一部分作为参数。如果您阅读同一教程的第 4 部分,您会看到 POST参数通过
request.POST
传入。在文档中,您将了解到可以编写处理 HTML 表单的生成和验证的 Form 类。If you read part 3 of the tutorial, you'll see that the view function expects parts of the URL itself as arguments. If you read part 4 of the same tutorial, you'll see that POST parameters come in via
request.POST
. Further in the documentation, you'll learn that you can write Form classes that handle both the generation and validation of HTML forms.它们将位于
request.POST
中,您可以像查询dict
一样对其进行查询they will be in
request.POST
, which you can query like you would adict
例如,如果您在
index.html
中提交POST
请求值,如下所示:那么,您可以在
index.html
中获取POST
请求值。 code>my_app1/views.py 如下所示。 *我的回答解释了更多:For example, if you submit the
POST
request values inindex.html
as shown below:Then, you can get the
POST
request values inmy_app1/views.py
as shown below. *My answer explains it more: