django形式的其他字段

编程入门 行业动态 更新时间:2024-10-25 20:22:31
本文介绍了django形式的其他字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在Django中创建一个表单。当我发布数据时,数据自然发送。我的问题是,我想向POST数据传递一个附加属性,该属性不是任何表单字段,而是一个附加属性。

I am creating a form in Django. When I POST the data, the data is naturally sent. My problem is, I want to pass an additional property to the POST data, that is not any of the form fields, but an additional one.

以便以后可以使用做类似(伪代码)的事情:

So that I can later do something like (pseudocode):

def form_view(request): if request.method == 'POST': form = MyForm(request.POST) if form.is_valid(): extra_field = form.cleaned_data['extra_field'] #or maybe extra_field = form.extra_field #... else: form = MyForm() #...

为了将额外的属性(不是字段而是简单的变量)传递给POST请求而可能起作用的任何事情。

Anything that might work in order to pass an extra property, which is not a field but a simple variable, to the POST request.

推荐答案

如果您希望将任何内容作为HTML的POST请求传递给django,则可以使用隐藏的输入

If you want to pass anything to django as a POST request from the HTML, you would use a hidden input

<input type="hidden" name="foo" value="bar" /> print request.POST['foo'] # out: bar

如果要从视图中修改python中的POST字典,请 copy()使其可变。

If you want to modify the POST dictionary in python from your view, copy() it to make it mutable.

mutable_post = request.POST.copy() mutable_post['foo'] = 'bar' form = MyForm(mutable_post)

更多推荐

django形式的其他字段

本文发布于:2023-11-02 13:58:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   形式   django

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!