Code examples for Django + SWFUpload?

Does anyone have simple code examples for Django + SWFUpload ? I am working fine on my PHP application, but Django gives me headaches.

+5
source share
3 answers

Unfortunately, I cannot give you very detailed code examples, but I have quite a lot of experience with SWFUpload + Django (for the photo-sharing site I am working on). Anyway, here are a few pointers that I hope will help you find DjSWF, fortunately :)

  • You want to use the cookie plugin (unless, of course, you use some session-based authentication [for example, django.contrib.authand take care who downloaded something).

    The cookie plugin sends data from cookies as a POST, so you have to find a way to return this back to request.COOKIES( process_requestmiddleware that searches settings.SESSION_COOKIE_NAMEin request.POSTfor specific URLs and dumps it in request.COOKIES, works great for this :)

  • Also remember that you must return something to the response body for SWFUpload in order to recognize it as a successful download attempt. I believe that this has changed in the latest beta version of SWFUpload, but in any case, it’s advisable to simply insert something there as “good”. For crashes, use something like HttpResponseBadRequestor the like.

  • , , request.FILES:)

- , , - , .

+17
+3

Django (, Firefox 302 Redirect).

, , cookie sessionid

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

, SWFUploadMiddleware ( Firefox), , sessionid .

In my internal view, which generates a page containing a load handler, I added sessionid to the context.

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

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

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

Now that I looked in SWFUploadMiddleware, I could see that sessionid was sent , and my downloads 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]
    # http://stackoverflow.com/questions/6634666/403-forbidden-error-on-swfupload-and-django
    # Fix for problem uploading images (403 error) in Firefox 20 and others
    if request.POST.has_key('csrftoken'):
            request.COOKIES['csrftoken'] = request.POST['csrftoken']
0
source

All Articles