django + uploadify - 不工作

时间:2010-05-12 18:20:33

标签: django uploadify

我正在尝试使用“github”上发布的示例链接为http://github.com/tstone/django-uploadify。而且我在上班时遇到了麻烦。你能帮助我吗?我一步一步地跟着,但是没有用。

访问“网址”/上传/ 唯一的回报是“真实”

settings.py的一部分

import os
PROJECT_ROOT_PATH = os.path.dirname(os.path.abspath(__file__))

MEDIA_ROOT = os.path.join(PROJECT_ROOT_PATH, 'media')
TEMPLATE_DIRS = (  os.path.join(PROJECT_ROOT_PATH, 'templates'))

urls.py

from django.conf.urls.defaults import *
from django.conf import settings
from teste.uploadify.views import *
from django.contrib import admin

admin.autodiscover()

urlpatterns = patterns('',
    (r'^admin/', include(admin.site.urls)),  
    url(r'upload/$', upload, name='uploadify_upload'),
)

views.py

from django.http import HttpResponse
import django.dispatch

upload_received = django.dispatch.Signal(providing_args=['data'])

def upload(request, *args, **kwargs):
    if request.method == 'POST':
        if request.FILES:
            upload_received.send(sender='uploadify', data=request.FILES['Filedata'])
    return HttpResponse('True') 

models.py

from django.db import models

def upload_received_handler(sender, data, **kwargs):
    if file:
        new_media = Media.objects.create(
            file = data,
            new_upload = True,
     )

    new_media.save()
    upload_received.connect(upload_received_handler, dispatch_uid='uploadify.media.upload_received')

class Media(models.Model):
    file = models.FileField(upload_to='images/upload/', null=True, blank=True)
    new_upload = models.BooleanField()

uploadify_tags.py

from django import template

from teste import settings 

register = template.Library()

@register.inclusion_tag('uploadify/multi_file_upload.html', takes_context=True)

    def multi_file_upload(context, upload_complete_url):
        """
        * filesUploaded - The total number of files uploaded
        * errors - The total number of errors while uploading
        * allBytesLoaded - The total number of bytes uploaded
        * speed - The average speed of all uploaded files
        """
        return {
            'upload_complete_url' : upload_complete_url,
            'uploadify_path' : settings.UPLOADIFY_PATH, # checar essa linha
            'upload_path' : settings.UPLOADIFY_UPLOAD_PATH,
        }    

模板 - uploadify / multi_file_upload.html

{% load uploadify_tags }{ multi_file_upload '/media/images/upload/' %}
<script type="text/javascript" src="{{ MEDIA_URL }}js/swfobject.js"></script>
<script type="text/javascript" src="{{ MEDIA_URL }}js/jquery.uploadify.js"></script>
<div id="uploadify" class="multi-file-upload"><input id="fileInput" name="fileInput" type="file" /></div>
<script type="text/javascript">// <![CDATA[
$(document).ready(function() {
    $('#fileInput').uploadify({
        'uploader'    : '/media/swf/uploadify.swf',
        'script'    : '{% url uploadify_upload %}',
        'cancelImg'   : '/media/images/uploadify-remove.png/',
        'auto'     : true,
        'folder'    : '/media/images/upload/',
        'multi'    : true,
        'onAllComplete' : allComplete
    });
});

function allComplete(event, data) {
 $('#uploadify').load('{{ upload_complete_url }}', {
  'filesUploaded'  : data.filesUploaded,
  'errorCount'   : data.errors,
  'allBytesLoaded'  : data.allBytesLoaded,
  'speed'     : data.speed
 });

 // raise custom event
 $('#uploadify') .trigger('allUploadsComplete', data);
}
// ]]</script>

3 个答案:

答案 0 :(得分:1)

但这正是你告诉它要做的 - 如果你不是POST,那么/upload/视图将只返回True。

我想你想要返回一个实际的渲染模板,可能包含{% multi_file_upload %}标签。

答案 1 :(得分:0)

您可能会发现以下网站有用: https://github.com/tstone/django-uploadify/wiki

答案 2 :(得分:0)

模板上的第1行,您的代码未正确关闭/打开 应该是

{% load uploadify_tags %}{% multi_file_upload '/myphotos/my-photos/'%}

据说,我自己得到了各种各样的错误......主要是用csrf验证。我看到tstone正在豁免csrf,但它仍然认为它应该在那里......而事实并非如此。所以根据django的文档https://docs.djangoproject.com/en/1.3/ref/contrib/csrf/#ajax,我添加了这些东西,以便csrf应该正常工作。但现在我得到405因为它试图发布自己。我会继续挖掘并希望找到解决方案。