【问题标题】:Not able to link a static web page using reverse_lazy无法使用 reverse_lazy 链接静态网页
【发布时间】:2018-05-14 00:58:24
【问题描述】:

我编写了一些代码来开发一个社交媒体网站。在那,我想重定向到我创建的主页外观。我试图为这个页面创建一个 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.

在这个项目中,共有三个应用程序。其中,“账户”是一款应用。在帐户中,我创建了包含三个文件的模板文件夹。(“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”页面。我已经创建了开始视图,但仍然显示如上所述的错误。请帮我解决这个问题。

【问题讨论】:

    标签: python html django django-views


    【解决方案1】:
    url(r'^start/$', views.MyView.as_view(), name="start")
    reverse_lazy("accounts:start")
    

    ^ 用于模式开始,$ 用于模式结束;您使用命名空间 url,请参阅Reversing namespaced URLs

    【讨论】:

    猜你喜欢
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-25
    • 2020-02-09
    • 2015-02-09
    • 1970-01-01
    • 2018-02-14
    • 2021-04-04
    相关资源
    最近更新 更多