Code samples for Django + SWFUpload?

Does anyone have any simple code samples for Django + SWFUpload? I have it working perfectly in my PHP application but Django is giving me headaches.

Asked by: Melanie983 | Posted: 06-12-2021

Answer 1

Unfortunately I can't give you any very detailed code samples, but I have quite a bit of experience with working with SWFUpload + Django (for a photo sharing site I work on). Anyway, here are a few pointers that will hopefully help you on your quest for DjSWF happiness :)

  1. You'll want to use the cookies plugin (if of course you are using some sort of session-based authentication [like django.contrib.auth, and care who uploaded what).

    The cookies plugin sends the data from cookies as POST, so you'll have to find some way of getting this back into request.COOKIES (process_request middleware that looks for a settings.SESSION_COOKIE_NAME in request.POST on specific URLs and dumps it into request.COOKIES works nicely for this :)

  2. Also, remember that you must return something in the response body for SWFUpload to recognize it as a successful upload attempt. I believe this has changed in the latest beta of SWFUpload, but anyway it's advisable just to stick something in there like 'ok'. For failures, make use of something like HttpResponseBadRequest or the like.

  3. Lastly, in case you're having trouble finding them, the uploaded file is in request.FILES :)

If you have anything perplexing I haven't covered, feel free to post something more detailed and I'll be happy to help.

Answered by: Lana341 | Posted: 07-01-2022

Answer 2

Django version of the samples for SWFUpload:

So long uploadify. Great idea but it is just buggy, especially on Windows.

Answered by: Arthur964 | Posted: 07-01-2022

Answer 3

The following is my Django-specific implementation for fixing this issue (i.e. my uploads were failing in Firefox with a 302 Redirect).

In my initial view that generates the page with the uploader on it, I looked at the cookies and found sessionid

ipdb> self.request.COOKIES
{'csrftoken': '43535f552b7c94563ada784f4d469acf', 'sessionid': 'rii380947wteuevuus0i5nbvpc6qq7i1'}

When I looked at what was being posted in the SWFUploadMiddleware (when using Firefox), I found that the sessionid was not being set.

In my intial view that generates the page that contains the upload handler, I added the sessionid to the context.

context['sessionid'] = self.request.session.session_key

In my swfuploader settings, I added sessionid to the post-params option as follows:

post_params: {... "sessionid": "{{ sessionid }}" ...},

Now, when I looked in the SWFUploadMiddleware, I could see the sessionid being posted, and my uploads started working if Firefox.

ipdb> request.POST 
<QueryDict: {... u'session_id': [u'rii380947wteuevuus0i5nbvpc6qq7i1'],...}>

For completeness, my SWFUploadMiddleware looks like this...

from django.conf import settings
from django.core.urlresolvers import reverse

class SWFUploadMiddleware(object):
    def process_request(self, request):
        if (request.method == 'POST') and (request.path == reverse('upload_handler')) and request.POST.has_key(settings.SESSION_COOKIE_NAME):
            request.COOKIES[settings.SESSION_COOKIE_NAME] = request.POST[settings.SESSION_COOKIE_NAME]
    # Fix for problem uploading images (403 error) in Firefox 20 and others
    if request.POST.has_key('csrftoken'):
            request.COOKIES['csrftoken'] = request.POST['csrftoken']

Answered by: Maya508 | Posted: 07-01-2022

Similar questions

python - swfupload failing in my django runserver

I have copied and pasted the code from into a django template, but when I upload a photo, the demo says "Server (IO) Error" before it actually uploads the entire file. The runserver is getting the request and returning a 200. Is there something I am missing here? What steps should I take to debug?

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python