Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 21, 2010 13:05:23

L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Load file into a FileField


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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 21, 2010 18:19:03

Chris L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Load file into a FileField


Liriela,

AFAIK it is not possible to programmatically populate a file input
field, due to the many security exploits this would enable. If it were
possible, it would be trivial to write a malicious script that would
upload any file from a user's PC, so long as you knew the path.

There are browser-specific exceptions, for example, in Netscape-based
browsers you can call
netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead")
(seehttp://www.mozilla.org/projects/security/components/signed-script-example.html).
This needs to be called from a signed script, and I believe will still
trigger a pop-up dialog to confirm the enhanced permission request. I
imagine this might be more trouble than it's worth though.

As an alternative testing method, you might consider using curl. You'd
do something like:

curlhttp://localhost:8000/submit/test-F id_title=Test -F
id_email=em...@test.org -F id_file=@/path/to/test.txt -u
username:password

Note the @ symbol in front of the value for file_id - that tells curl
that the value is a file, not a string.

I've only really used this method when testing an API which returned
JSON data, which is easily viewable in the console. If your view is
returning HTML, you might pipe the output to a file to open in a
browser.

Hope that helps,

Chris

On Dec 21, 5:14 am, Liriela <kajamilanow...@gmail.com> wrote:
> 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 athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 17th of August
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support