0

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

user1597122
  • 327
  • 1
  • 6
  • 24
  • 1
    I don't understand sorry – catherine Mar 10 '13 at 08:04
  • I think you may need to re-write your question a bit. What do you mean when you say 'I can't initial FileField' or 'when I initial forms'? 'initial' is an adjective not a verb. (And since you've correctly used the word 'initialize' earlier in your question, it seems fair to assume that's not what you mean). – Aidan Ewen Mar 10 '13 at 08:27
  • I'm really sorry if i asked my question not fine. i rewrite it and wish you understand. thanks a lot – user1597122 Mar 10 '13 at 09:20
  • Can you just have a sample output that you want to show in your template than putting a long explanation? Sorry but I can't understand it totally. – catherine Mar 10 '13 at 14:22
  • I edited my question. I wish it would be clear at this time. thanks – user1597122 Mar 10 '13 at 16:04

2 Answers2

1

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

galuszkak
  • 513
  • 4
  • 25
donkeyboy72
  • 1,883
  • 10
  • 37
  • 55
0

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.

Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
  • i fill list items from data base. in this example i write a.png and ... to make it simple, but in reality i fill list from fileNames which are stored in data base. sorry if i couldn't exactly ask my question. – user1597122 Mar 10 '13 at 10:26
  • thanks a lot... You know in my situation there are forms in formset and list. I don't think i can zip them, can I? :"> – user1597122 Mar 10 '13 at 16:04