Hi there,

I am working now on a web server and come up with a problem. I want to
have on my site a button which will load test data into a form -> for
example -> a form has fileds: name and email -> the button will load
into the fields values defined by me. That is not a problem for all
the data that is served by request.POST. But the problem shows up when
I have a FileField in my forms and I want to load a file into that
field. The solution that I have found probably loads somehow the file
into request.FILES dictionary (so the file is bound to the form) -
because there is no non-field error shown, but after pressing the
submit button the non field error occurs (so the file is lost). And
the name of the file is not shown in the form. (I am using django
version 1.2.3 and python version 2.6.6 )


 Here are my forms, views and template for that:

forms.py
(...)
class Test(forms.Form):
    ''''''

    title          = forms.CharField(max_length = 30, required = True)
    email       = forms.EmailField(required=False)
    test_file    = forms.FileField()

views.py

from django.core.files.uploadedfile import SimpleUploadedFile
(...)
def load_test_analysis(request):
    """ """
    f = open('/path/test.txt','r')
   post_data =  {'title':'Test ',
                        'email' : 't...@test.org'}
    file_data = {'alignment': SimpleUploadedFile('test.txt',
f.read())}
    form = Test(post_data, file_data)
    return render_to_response('test.html', {'form': form})

test.html

{% extends 'master.html' %}

{% block content %}

<div id="content">

<table>
    <tr>
        <td>
            <h1>Teste</h1>
        </td>
        <td align="right">
             <input type="button" onclick="window.location.href='/
submit/model/test/'" value="Load test data"/>
        </td>
    </tr>
</table>

<form name="myForm" method="post" enctype="multipart/form-data"
action="/submit/test/">

<p><label for="id_title">Title:</label> {{ form.title }}
<ul class="errorlist"><li>{{ form.title.errors }}</li></ul>
</p>

<p><label for="id_file">File:</label> {{ form.file }}</p>
<ul class="errorlist"><li>{{ form.file.errors }}</li></ul>
<ul class="errorlist"><li>{{ form.non_field_errors }}</li></ul>

<p><label for="id_email">Email:</label> {{ form.email }}
<ul class="errorlist"><li> {{form.email.errors}} </li></ul>
</p>

<input id="submit" class="submit" type="submit" value="Test" />
<input type="reset" value="Reset" name="inputForm"/>
</form>

</div>

{% endblock %}


Hope someone will be able to help me with that.

Best,
Liriela

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to