Django ajax 上传程序示例
Django ajax uploader example
我尝试使用 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>
您忘记将应用名称 bbb
添加到 url:
url(r'ajax-upload$', 'bbb.views.import_uploader', name="my_ajax_upload"),
我尝试使用 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>
您忘记将应用名称 bbb
添加到 url:
url(r'ajax-upload$', 'bbb.views.import_uploader', name="my_ajax_upload"),