I'm trying to write the code and implement a file upload screen based
on this document:

http://docs.djangoproject.com/en/1.2/topics/http/file-uploads

I'm getting the following error:

Forbidden (403)
CSRF verification failed. Request aborted.

Help
Reason given for failure:

    CSRF token missing or incorrect.
    In general, this can occur when there is a genuine Cross Site
Request Forgery, or when Django's CSRF mechanism has not been used
correctly. For POST forms, you need to ensure:

•The view function uses RequestContext for the template, instead of
Context.
•In the template, there is a {% csrf_token %} template tag inside each
POST form that targets an internal URL.
•If you are not using CsrfViewMiddleware, then you must use
csrf_protect on any views that use the csrf_token template tag, as
well as those that accept the POST data.
You're seeing the help section of this page because you have DEBUG =
True in your Django settings file. Change that to False, and only the
initial error message will be displayed.

You can customize this page using the CSRF_FAILURE_VIEW setting.

I have tried coding the csrf token ({% csrf_token %}) on my new screen
as well as leaving it off and its presence does not seem to matter, at
least by itself, because I get the same error whether its present or
not. My screen so far is coded like this:

<body>
<h1>POLL APPLICATION FILE UPLOAD SCREEN</h1>
<br />
<form action="/polls/uploadfile/" method="POST" enctype="multipart/
form-data">
{{ form.title }}
<br />
{{ form.filename.label }}
{{ form.filename }}
{{ form.filename.errors }}

<br /><br />
<input type="submit" value="Upload File" />
<br /><br />
</form>
<br /><br />
<a href="{{ mainmenuurl }}">{{ mainmenutext }}</a>
<br /><br />
</body>

and though I've defined and added the form.title field to the screen
the document does not seem to mention what it's supposed to be for or
how its used, though it shows it coded in the upload form. My view
code for this screen looks like this:

def process_uploaded_file(file):
    #destination = open('C:/users/hversemann/desktop/testfile.txt', 'wb
+')
    destination = open('C:/users/hversemann/desktop/testfile.txt',
'w')
    for chunk in file.chunks():
        destination.write(chunk)
    destination.close()

def uploadfile(request):
    mainmenuurl = "/polls/mainmenu/"
    mainmenutext = "Main Menu"
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            process_uploaded_file(request.FILES['file'])
            confirmmessage = "File has been uploaded!"
            dctnry = { 'confirmmessage': confirmmessage,
'mainmenuurl': mainmenuurl, 'mainmenutext': mainmenutext }
            return render_to_response('polls/confirm.html',
                dctnry,
                context_instance=RequestContext(request))
        else:
            dctnry = { 'form': form, 'mainmenuurl': mainmenuurl,
'mainmenutext': mainmenutext }
            return render_to_response('polls/uploadfile.html',
                dctnry,
                context_instance=RequestContext(request))
    else:
        form = UploadFileForm()
        dctnry = { 'form': form, 'mainmenuurl': mainmenuurl,
'mainmenutext': mainmenutext }
        return render_to_response('polls/uploadfile.html', {'form':
form})

I've coded as much of of this as possible from right out of the
document. So I'm not sure based on the error exactly where the problem
may be. Thanks in advance for the help.





-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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