Note that there are some explanatory texts on larger screens.

plurals
  1. POfile upload with django
    primarykey
    data
    text
    <p>I am newbie working with django, I am trying to create a form that permits the user to upload a file. The code I used is pretty much the same as from the djangoproject tutorial but it didn't work. my code is as follows:</p> <p>for views: </p> <pre><code>from django import forms from django.shortcuts import render_to_response from django.http import HttpResponseRedirect def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) return HttpResponseRedirect('/') else: form = UploadFileForm() return render_to_response('upload_file.html', {'form': form}) def handle_uploaded_file(f): destination = open('home/dutzy/Desktop/mysite/name.txt', 'wb+') for chunk in f.chunks(): destination.write(chunk) destination.close() </code></pre> <p>models:</p> <p>from django.db import models</p> <pre><code>from django import forms class UploadFileForm(forms.Form): title = forms.CharField(max_length=50) file = forms.FileField() </code></pre> <p>and the template:upload_file.html </p> <pre class="lang-html prettyprint-override"><code>&lt;form enctype="multipart/form-data" method="post" action="/upload_file/"&gt; &lt;table&gt; &lt;tr&gt; &lt;td&gt; &lt;b&gt; {{ form.file.label_tag }}&lt;/b&gt; {{ form.file}} &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;input type="hidden" value="title" name="title" id="title" /&gt; &lt;input type="submit" value="Save" id="Save"/&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>i am testing my code on the django development server. the error is:<strong>global name 'UploadFileForm' is not defined</strong>, altough i suspect there are also other problems. i have configured the urls.py as: <code>(r'^upload/$', 'mysite.upload.views.upload_file')</code></p> <p>Could someone please take a look at my code and point me in the right direction?</p> <p>Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload