Django ajax uploader示例

时间:2015-01-22 03:58:41

标签: python ajax django file-upload file-transfer

我尝试使用django-ajax-uploader创建一个示例但我上传失败了,错误:

ImportError: No module named views

我如何解决这个错误,或者是什么是视图模块,是关于urls.py的?

Urls.py

from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'bft.views.home', name='home'),
    # url(r'^bft/', include('bft.foo.urls')),

    # Uncomment the admin/doc line below to enable admin documentation:
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')),

    # Uncomment the next line to enable the admin:
    # url(r'^admin/', include(admin.site.urls)),
    url(r'start$', 'bbb.views.start', name="start"),
    url(r'ajax-upload$', 'views.import_uploader', name="my_ajax_upload"),
)

views.py

from django.middleware.csrf import get_token
from django.shortcuts import render_to_response
from django.template import RequestContext

from ajaxuploader.views import AjaxFileUploader


def start(request):
    csrf_token = get_token(request)
    return render_to_response('import.html',
        {'csrf_token': csrf_token}, context_instance = RequestContext(request))

import_uploader = AjaxFileUploader()
# Create your views here.

import.html

 <!doctype html>
        <head>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" ></script>
            <script src="{{ STATIC_URL }}ajaxuploader/js/fileuploader.js" ></script>
            <link href="{{ STATIC_URL }}ajaxuploader/css/fileuploader.css" media="screen" rel="stylesheet" type="text/c$
            <script>
                $(function(){
                var uploader = new qq.FileUploader({
                    action: "/ajax-upload",
                    element: $('#file-uploader')[0],
                    multiple: true,
                    onComplete: function(id, fileName, responseJSON) {
                        if(responseJSON.success) {
                            alert("success!");
                        } else {
                            alert("upload failed!");
                        }
                    },
                    onAllComplete: function(uploads) {
                        // uploads is an array of maps
                        // the maps look like this: {file: FileObject, response: JSONServerResponse}
                        alert("All complete!");
                    },
                    params: {
                        'csrf_token': '{{ csrf_token }}',
                        'csrf_name': 'csrfmiddlewaretoken',
                        'csrf_xname': 'X-CSRFToken',
                    },
                });
                });
            </script>
        </head>
    <body>
        <div id="file-uploader">
            <noscript>
                <p>Please enable JavaScript to use file uploader.</p>
            </noscript>
        </div>
    </body>
    </html>

1 个答案:

答案 0 :(得分:1)

您忘记将应用名称bbb添加到网址:

url(r'ajax-upload$', 'bbb.views.import_uploader', name="my_ajax_upload"),