在FileView的get_success_URL方法中,我使用了' display:redirect'作为要重定向的URL。但我收到错误
Reverse for 'redirect' with arguments '()' and keyword arguments '{'pk': 15}' not found. 1 pattern(s) tried: ['display/success/']
请求方法:POST 请求网址:http://127.0.0.1:8000/display/upload/ Django版本:1.10.dev20160512164014 异常类型:NoReverseMatch 例外值:
我错过了什么吗?
display / views.py
class FileView(FormView):
template_name = 'display/upload.html'
form_class = FileForm
def form_valid(self, form):
file_upload = FileModel(file=self.get_form_kwargs().get('files')['file'])
file_upload.save()
self.id = file_upload.id
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse('display:redirect', kwargs={'pk': self.id})
def redirect(request):
return render(request,"display/success.html")
显示/ urls.py
from django.conf.urls import url
from django.conf.urls.static import static
from django.conf import settings
from . import views
from display.views import FileView
urlpatterns = [
url(r'^start/', views.initial,name='home'),
url(r'^upload/',FileView.as_view(),name='upload'),
url(r'^success/',views.redirect,name='redirect'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
答案 0 :(得分:0)
看看这一行:
Reverse for 'redirect' with arguments '()' and keyword arguments '{'pk': 15}' not found.
错误消息告诉您错误。您已经传递了一个关键字参数,当它试图反转/找到它没有的网址时,因为您将网址指定为:
url(r'^success/',views.redirect,name='redirect')
哪个不接受任何参数。一种可能性是将你的网址改为:
^success/(?P<pk>\d+)/$