Django: custom constructor for form class, problem with data access from .POST request

I wrote my own constructor for the 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 the form, in my opinion 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 create the form this way, in my opinion I am not passing coto and naglowek , so when I call form.is_valid () -> it returns false.

A template that looks like this:

<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 wouldn't say it clearly enough, but I'll try my best:

When I submit this form, I need to know which submit button was clicked - many are assigned to the same form. From what I know, when I assign an id to submit a button, it should be available in the mail, right? The trick is that it is not available.

I have two questions: * What should I do if I want to know which submit button is clicked? Is assigning a name the only way? * Is there a mistake in my logic?

-1


a source to share


3 answers


Your question is a mess. There the code is and there is an edit with another question. The editing 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 different names or values ​​(or both). Here's our code that uses different 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 such a thing:

            if request.POST['action'] == "Delete":

      

to change the action based on the button.

+2


a source


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

      



I think.

0


a source


You have overridden the default form constructor and changed the order of your parameters. Therefore, you need to instantiate your custom form with explicit arguments:

form = UploadForm(data=request.POST, files=request.FILES, coto=..., naglowek=...)

      

0


a source







All Articles