我正在使用Python 3.6
和Django 2.0.5
开展项目。我有一个名为register_client.html
的模板,我想用它来渲染一个简单的页面,将新客户注册到Client
模型。
以下是我的完整urlpatterns
:
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^logout/$', views.user_logout, name='logout'),
re_path(r'^$', views.home, name='home'),
re_path(r'^home/$', views.home, name='home'),
re_path(r'^about/$', views.about, name='about'),
re_path(r'^blog/$', views.blog, name='blog'),
re_path(r'^contact/$', views.contact, name='contact'),
re_path(r'^lawsuits/$', views.lawsuits_list, name='lawsuits'),
re_path(r'^my_profile/$', views.my_profile, name='my_profile'),
re_path(r'^register_client/$', views.register_client, name='register_client'),
re_path(r'^client/(\d+)/$', views.client, name='client'),
re_path(r'^client/(\d+)/lawsuits/$', views.lawsuits_list, name='lawsuits'),
re_path(r'^client/(\d+)/edit/$', views.edit_client, name='edit_client'),
re_path(r'^client/(\d+)/remove/$', views.remove_client, name='remove_client'),
re_path(r'^lawsuit/(\d+)/$', views.lawsuit, name='lawsuit'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
应该向http://localhost/register_client发送请求的模式应为re_path(r'^register_client/$', views.register_client, name='register_client')
。
这是应该适当渲染模板的视图:
def register_client(request):
if request.user.is_authenticated and request.user.is_staff:
if request.method == "POST":
form = ClientForm(request.POST, request.FILES)
if form.is_valid():
# register a new client
...
else:
print(form.errors)
form = ClientForm()
return render(request, 'register_client.html', {'form': form})
return HttpResponseRedirect('/home/') # not_permitted view
通过上述配置,Django给了我一个NoReverseMatch at /register_client/
。它进一步声明它正在查找名为client
的视图,这是我views.py
- Reverse for 'client' with arguments '('',)' not found. 1 pattern(s) tried: ['client/(\\d+)/$']
我的想法和地点已经不多了,可能会找出原因。任何帮助或提示将受到高度赞赏,提前感谢。