views:

545

answers:

3

Hello.

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.

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.

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?

A: 
request.POST['coto']
request.POST['naglowek']

I guess.

SilentGhost
I tried - and it's not working. I might messed this one somewhere, but I dunno where. Thank You for trying to help.
praavDa
may be you could check your html to see what is actually being submitted?
SilentGhost
and what exactly "it's not working" means?
SilentGhost
By "it's not working" I mean, that I get error "Key 'coto' not found in <QueryDict: {u'slownik': [u'Wyślij']}>"
praavDa
A: 

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=...)
Alex Koshelev
Alex, I know that I redefined parameters order - the trick here is that I cannot access 'coto' and 'naglowek' in POST dictionary.
praavDa
+2  A: 

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.

S.Lott