尽管在urls.py中包含了url名称,但我收到了以下错误:
Reverse for 'upload' with arguments '()' and keyword arguments '{}' not found.0 pattern(s) tried: []
我已将网址名称设为'上传'在urls.py中 最初home.html应该发布到'上传'这是我包含的FileView:
url(r'^upload/',FileView.as_view(),name='upload')
views.py
def initial(request):
return render(request,'display/home.html')
class FileView(FormView):
template_name = 'display/upload.html'
form_class = FileForm
success_url = 'success/'
def form_valid(self, form):
file_upload = FileModel(
fileup=self.get_form_kwargs().get('files')['fileup'])
#results = FileForm.objects.filter(Q(name__icontains=your_search_query) | Q(startdate__icontains=your_search_query) | Q(enddate__icontains=your_search_query)).order_by('pub_date')
file_upload.save()
self.id = file_upload.id
return render_to_response('display/success.html')
return HttpResponseRedirect(self.get_success_url())
def get_success_url(self):
return reverse('file_upload', kwargs={'pk': self.id})
def redirect(request):
return render(request,"display/success.html")
...
urls.py
from django.conf.urls import url
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='success'),
#url(r'^upload',views.upload,name='upload'),
]
模板 home.html的
<!DOCTYPE html>
<html>
<head>
<title>CSV FILE UPLOAD</title>
</head>
<body>
<form enctype="multipart/form-data" method="POST" action="{% url 'upload' %}">
{% csrf_token %}
<input type="submit" name="submit" />
</form>
</body>
答案 0 :(得分:1)
这个urls.py
看起来不像根网址...可能是根网址内的包含内容丢失了吗?
示例:
urlpatterns = [
url(r'^display/', include('display.urls', namespace='display')),
]
如果包含这样的内容,您应该将upload
网址称为:
<form enctype="multipart/form-data" method="POST" action="{% url 'display:upload' %}">
其他可能的解决方案,请尝试在网址正则表达式的末尾添加$
:
from django.conf.urls import url
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='success'),
]