user1597122
user1597122

Reputation: 327

loop over forms in formset and a list at same time + FileField required error :django

I have a formset which i initialize it in view. one of the form's fields is FileField. and I have to show user the name of his/her previous file name. Because i can't initialize FileField, i want to send file names in list. (I mean for example when you have a Charfield, you can initialize it in views and when you render to template, you will see an input filled with that data, but when you have file upload field you can't filled it in views and sends to template). i don't know how i can loop over forms in formset and list at the same time. And the other thing is when i initialize forms in formset and render to template (I mean for example write data['form-0-Team']='team1' but i can't write data['form-0-Team']='a.png' , so when i render to template, i see field named 'Team' is filled (value=team1) and field named 'File' is not filled and the error 'thid field is required' id shown. ) although it's the first time i'm visiting this page and my method isn't POST. (USUALLY form errors are shown when user clicks on submit and in views it checks if request.method == 'POST', then checks if form.is_valid, it return to template and shows errors, but in mine, it shows errors at the first time euser is visiting the page and before he/she posts data). I wish i could say my problem. can you please guide me solve this? really thanks.

def myFunc(request):
    flagFormSet = formset_factory(FlagFileBaseForm)
    if request.method == 'POST':
        formset = flagFormSet(request.POST, request.FILES)
        if formset.is_valid():
          # do s.th
    else:
         data = {
                    'form-TOTAL_FORMS': 5,
                    'form-INITIAL_FORMS': u'0',
                    'form-MAX_NUM_FORMS': u'',
                    # add initial form data to it
                }
         list=['a.png', 'b.png', 'c.png', 'd.png' , 'f.png']

         formset = flagFormSet(data)
    return render_to_response('myPage.html', RequestContext(request, { 'formset': formset, 'list':list}))

and my template:

<form method="post" action="" enctype="multipart/form-data">
{{ formset.management_form }}
    {% for form in formset.forms %}
        <div class="form">
           <div class="form-row Team">
              <div>
                <label class="required" for="id_Team">Team:</label>
                    {{ form.Team }}
                    {{ form.Team.errors }}
              </div>
           </div>                           
           <div class="form-row File">
               <div>
                  <label class="required" for="id_File">File:</label>
                       {{ form.File }}
                       {{ form.File.errors }}
               </div>

    #here i want show the name of previous file

           </div>
        </div>
    {% endfor %}
</form>

EDIT: current result (while request is not post, it shows error) enter image description here

desired result (form without error with file names) enter image description here

Upvotes: 0

Views: 2532

Answers (2)

donkeyboy72
donkeyboy72

Reputation: 1953

In Django Templates, if you are trying to render a form that has a FileField. You must pass replace

<form method="post" action="">

with

<form method="POST" enctype="multipart/form-data">

https://docs.djangoproject.com/en/dev/ref/forms/api/#binding-uploaded-files-to-a-form

Upvotes: 1

Aidan Ewen
Aidan Ewen

Reputation: 13328

I'm afraid I don't really understand your question, so apologies if this doesn't help..

If your list is arbitary (as it looks from your question), then you could use django's built in forloop counter to construct your file names -

{% for form in formset %}
    {{ form }}
    <input type="text" name="file_name" value="{% forloop.counter %}.png">
{% endfor %}

Alternatively have a look at python's zip function. You could use it to build an object that includes both the names and the forms and pass that to your template.

Upvotes: 0

Related Questions