django - 从 HTML
我看过一些关于如何从 Django 中的 HTML 语句收集数据的文档,但对我来说都不是很清楚。有人有一个真实的工作示例可以分享吗?
就我而言,我的模板文件中有类似这样的内容:
<select title="my_options">
<option value="1">Select value 1</option>
<option value="2">Select value 2</option>
</select>
views.py 中为了收集选定的值会做什么?谢谢你!
I've seen a couple of documents on how to collect the data from a HTML statement in Django but none of them were very clear to me. Does anybody have a real working example to share?
In my case I have something like this in my template file:
<select title="my_options">
<option value="1">Select value 1</option>
<option value="2">Select value 2</option>
</select>
What goes in the views.py in order to collect the selected value? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果是 GET 请求,则为
request.GET['my_options']
。如果是 POST,则为request.POST['my_options']
。这将是一个字符串,"1"
或"2"
(或"
无论哪种方式,最好使用 Django 表单框架 可以省去编写 HTML 和清理返回值的麻烦。
If it's a GET request,
request.GET['my_options']
. If it's a POST, thenrequest.POST['my_options']
. This will be a string, either"1"
or"2"
(or"<script>alert('I hacked you!')</script>"
)Either way, it's probably better to use the Django forms framework to save you the hassle of writing the HTML, and sanitizing the returned values.
通过 POST 管理数据
Django 中的表单可能有用的一件事是,如果您对 URL 进行 POST 或只是加载它,则可以做不同的事情:
有 2 个
render_to_response
以防您需要做不同的事情如果视图刚刚加载或收到 POST 的情况。通过 GET 管理数据
Manage data via POST
One thing that may be useful with forms in Django is to make different things if you make a POST to the URL or just load it:
There are 2
render_to_response
in case you need to do different things if the view was just loaded or receive a POST.Manage data via GET