我正在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形式的其他字段
发布评论