I have written custom constructor for a form, the whole form class looks like this:
我为表单编写了自定义构造函数,整个表单类如下所示:
class UploadForm(forms.Form):
file = forms.FileField(label = "Plik")
def __init__(self, coto, naglowek, *args, **kwargs):
super(UploadForm, self).__init__(*args, **kwargs)
self.coto = coto
self.naglowek = naglowek
When submitting form, in my view, I have something like
提交表格时,在我看来,我有类似的东西
if request.method == 'POST':
form = UploadForm(request.POST, request.FILES)
if form.is_valid():
add_form(request.FILES['file'])
return HttpResponseRedirect('uploaded/')
The problem is, that when I am creating form in this way in my view, I am not passing coto and naglowek, so when I am calling form.is_valid() --> it returns false.
问题是,当我在我的视图中以这种方式创建表单时,我没有传递coto和naglowek,所以当我调用form.is_valid() - >时它返回false。
The template which passess it looks like:
通过它的模板看起来像:
<table class="uploadform">
<form action="." method="POST" enctype="multipart/form-data">
{% for form in forms %}
<tr>
<td>{{ form.naglowek }}</td>
<td>{{ form.file }}</td>
<td><input type="submit" name="{{ form.coto }}" id="{{ form.coto }}" value="Wyślij"></td>
</tr>
{% endfor %}
</form>
</table>
I would be grateful for any suggestions.
我会很感激任何建议。
[EDIT] I might not say this clearlly enough, but I will try my best:
[编辑]我可能不会说得这么清楚,但我会尽我所能:
When I am submitting this form, in view, I need to know which submit button was pressed - I have many of them assigned to single form. From what I know, when I am assigning id to submit button, it should be availible in post, right? The trick is, that it is not availible.
当我提交此表单时,在视图中,我需要知道按下了哪个提交按钮 - 我将其中许多分配给单个表单。据我所知,当我分配id提交按钮时,它应该在帖子中可用,对吧?诀窍是,它不可用。
I have two questions: * What needs to be done, If I want to know which submit button was pressed? Is assigning the name the only way? * Is there any error in my logic?
我有两个问题:*需要做什么,如果我想知道按下了哪个提交按钮?分配名称是唯一的方法吗? *我的逻辑中有错误吗?
3 个解决方案
#1
Your question is a mess. There's code and there's an edit with another question. The edit question has nothing to do with the title.
你的问题很乱。有代码,还有另一个问题的编辑。编辑问题与标题无关。
Please update this question to be your real question.
请将此问题更新为您真正的问题。
If you have multiple submit buttons, you must give them distinct names or values (or both). Here's our code which uses distinct values to distinguish which button was clicked.
如果您有多个提交按钮,则必须为它们指定不同的名称或值(或两者)。这是我们的代码,它使用不同的值来区分单击了哪个按钮。
<form method="post" action="." enctype="multipart/form-data">
<input type="hidden" name="object_id" value="{{e.id}}"/>
{% ifequal object.workflow "uploaded" %}
<input type="submit" name="action" value="Validate"/>
<br/>
<input type="submit" name="action" value="Delete"/>
{% endifequal %}
{% ifequal object.workflow "validated" %}
<input type="submit" name="action" value="Load"/>
{% endifequal %}
{% ifequal object.workflow "processed" %}
<input type="submit" name="action" value="Undo"/>
{% endifequal %}
{% ifequal object.workflow "failed" %}
<input type="submit" name="action" value="Validate"/>
{% endifequal %}
</form>
The view function has this kind of thing:
视图函数有这样的事情:
if request.POST['action'] == "Delete":
to change the action based on the button.
根据按钮更改操作。
#2
request.POST['coto']
request.POST['naglowek']
I guess.
#3
You've redefined default form constructor and change its parameters order. So you have to instantiate your custom form with explicit naming of arguments:
您已重新定义默认表单构造函数并更改其参数顺序。因此,您必须使用显式命名参数来实例化自定义表单:
form = UploadForm(data=request.POST, files=request.FILES, coto=..., naglowek=...)
#1
Your question is a mess. There's code and there's an edit with another question. The edit question has nothing to do with the title.
你的问题很乱。有代码,还有另一个问题的编辑。编辑问题与标题无关。
Please update this question to be your real question.
请将此问题更新为您真正的问题。
If you have multiple submit buttons, you must give them distinct names or values (or both). Here's our code which uses distinct values to distinguish which button was clicked.
如果您有多个提交按钮,则必须为它们指定不同的名称或值(或两者)。这是我们的代码,它使用不同的值来区分单击了哪个按钮。
<form method="post" action="." enctype="multipart/form-data">
<input type="hidden" name="object_id" value="{{e.id}}"/>
{% ifequal object.workflow "uploaded" %}
<input type="submit" name="action" value="Validate"/>
<br/>
<input type="submit" name="action" value="Delete"/>
{% endifequal %}
{% ifequal object.workflow "validated" %}
<input type="submit" name="action" value="Load"/>
{% endifequal %}
{% ifequal object.workflow "processed" %}
<input type="submit" name="action" value="Undo"/>
{% endifequal %}
{% ifequal object.workflow "failed" %}
<input type="submit" name="action" value="Validate"/>
{% endifequal %}
</form>
The view function has this kind of thing:
视图函数有这样的事情:
if request.POST['action'] == "Delete":
to change the action based on the button.
根据按钮更改操作。
#2
request.POST['coto']
request.POST['naglowek']
I guess.
#3
You've redefined default form constructor and change its parameters order. So you have to instantiate your custom form with explicit naming of arguments:
您已重新定义默认表单构造函数并更改其参数顺序。因此,您必须使用显式命名参数来实例化自定义表单:
form = UploadForm(data=request.POST, files=request.FILES, coto=..., naglowek=...)