我尝试上传图片。最多选择照片它工作正常,但当我点击上传时,它给出了以下错误:
Django Version: 1.10
Python Version: 3.5.1
Installed Applications:
['polls.apps.PollsConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback:
File "D:\virtualEnv\lib\site-packages\django\core\handlers\exception.py" in inner
39. response = get_response(request)
File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)
File "D:\virtualEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\virtualEnv\mysite\polls\views.py" in home
103. return HttpResponseRedirect(reverse('imageupload'))
File "D:\virtualEnv\lib\site-packages\django\urls\base.py" in reverse
91. return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "D:\virtualEnv\lib\site-packages\django\urls\resolvers.py" in _reverse_with_prefix
389. (lookup_view_s, args, kwargs, len(patterns), patterns)
Exception Type: NoReverseMatch at /polls/upload/
Exception Value: Reverse for 'imageupload' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []
我尝试了以下模型:
class Upload(models.Model):
pic = models.ImageField("Image", upload_to="images/")
upload_date=models.DateTimeField(auto_now_add =True)
views.py
from django.shortcuts import render
from uploader.models import UploadForm,Upload
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.
def home(request):
if request.method=="POST":
img = UploadForm(request.POST, request.FILES)
if img.is_valid():
img.save()
return HttpResponseRedirect(reverse('imageupload'))
else:
img=UploadForm()
images=Upload.objects.all()
return render(request,'home.html',{'form':img,'images':images})
模板:
<div style="padding:40px;margin:40px;border:1px solid #ccc">
<h1>picture</h1>
<form action="#" method="post" enctype="multipart/form-data">
{% csrf_token %} {{form}}
<input type="submit" value="Upload" />
</form>
{% for img in images %}
{{forloop.counter}}.<a href="{{ img.pic.url }}">{{ img.pic.name }}</a>
({{img.upload_date}})<hr />
{% endfor %}
</div>
和ModelForm:
class UploadForm(ModelForm):
class Meta:
model = Upload
民意调查网址:
url(r'^upload/$', views.home, name='imageupload'),
mysite网址:
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
有人可以建议解决问题的方法。