我已经编写了一些代码来开发社交媒体网站。在那,我想重定向到我创建的主页look-a-like。我试图为这个页面创建一个URL,但是没有用。我收到的错误如下:
NoReverseMatch at /accounts/signup/
Reverse for 'start' not found. 'start' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/accounts/signup/
Django Version: 1.11.7
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'start' not found. 'start' is not a valid view function or pattern name.
在这个项目中,有三个应用程序。其中,“帐户”是一个应用程序。在帐户中,我创建了包含三个文件的templates文件夹(“login.html,signup.html和start.html”)。 views.py 文件:
from django.contrib.auth import login, logout
from django.core.urlresolvers import reverse_lazy
from django.views.generic import CreateView
from . import forms
from django.views.generic import TemplateView
class MyView(TemplateView):
template_name = 'accounts/start.html'
class SignUp(CreateView):
form_class = forms.UserCreateForm
success_url = reverse_lazy("start")
template_name = "accounts/signup.html"
urls.py 文件为:
from django.conf.urls import url
from django.contrib.auth import views as auth_views
from . import views
app_name = 'accounts'
urlpatterns = [
url(r"login/$", auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
url(r"logout/$", auth_views.LogoutView.as_view(), name="logout"),
url(r"signup/$", views.SignUp.as_view(), name="signup"),
url(r"start/$", views.MyView.as_view(), name="start")
]
如何从注册页面重定向到“start.html”页面。我已经创建了开始视图,但仍然显示如上所述的错误。请帮帮我。
答案 0 :(得分:1)
url(r'^start/$', views.MyView.as_view(), name="start")
reverse_lazy("accounts:start")
模式开始为 ^
,模式结束为$
;您使用命名空间网址,请参阅Reversing namespaced URLs;