django.urls.exceptions.NoReverseMatch:反向' sign_up'未找到。 ' sign_up'不是有效的视图函数或模式名称

时间:2017-10-17 17:17:16

标签: python django

Django版本1.11.5,

views.py

class SignupPage(CreateView):
    form_class = forms.UserSignupForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

网站/ urls.py

from django.conf.urls import url
from django.contrib.auth import views as auth_views
from django.contrib.auth.views import LoginView,LogoutView
from . import views

app_name = 'website'

urlpatterns = [

    url(r'^login/', auth_views.LoginView.as_view(template_name='signin_2_w.html'), name='login'),
    url(r'^logout/$', auth_views.LogoutView.as_view(), name='logout'),
    url(r'^signup/', views.SignupPage.as_view(), name='sign_up'),

forms.py

class UserSignupForm(UserCreationForm):

    class Meta:
        fields = ('username','email','password1','password2')


        model = get_user_model()

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        self.fields['username'].label = "Display Name"
        self.fields['email'].label = "Email Address"
        self.fields['password1'].label = "Password"
        self.fields['password2'].label = "Confirm Password"

凸出/ urls.py

urlpatterns = [

    url(r'^admin/', admin.site.urls),
    url(r'^$',views.HomePage.as_view(),name='home'),
    url(r'^website/',include('website.urls',namespace='website')),
    url(r'^website/',include('django.contrib.auth.urls')),
    url(r'^website/',views.StreamyePage.as_view(),name='streamye'),
    url(r'^thanks/$',views.ThanksPage.as_view(),name='thanks'),
    url(r'^congrats/$',views.CongratsPage.as_view(),name='congrats'),
    url(r'^aboutus/$',views.AboutusPage.as_view(),name='about_us'),

HTML

<ul class="nav navbar-nav navbar-right">
            {% if user.is_authenticated %}
            <li><a href="{% url 'logout' %}">Logout</a></li>
            {% else %}
             <li><a href="{% url 'login' %}">Login</a></li>
             <li><a href="{% url 'sign_up' %}">Signup</a></li>
            {% endif %}
          </ul>

在上面的代码中,注册页面给我一个错误 - &#34; django.urls.exceptions.NoReverseMatch:反向&#39; sign_up&#39;未找到。 &#39; sign_up&#39;不是有效的视图函数或模式名称。&#34;

请有人帮助我,我错了! 提前谢谢!

2 个答案:

答案 0 :(得分:2)

您必须修复模板中的链接以定位website网址:

 {% if user.is_authenticated %}
        <li><a href="{% url 'website:logout' %}">Logout</a></li>
        {% else %}
         <li><a href="{% url 'website:login' %}">Login</a></li>
         <li><a href="{% url 'website:sign_up' %}">Signup</a></li>
 {% endif %}

答案 1 :(得分:0)

OP在THEIR网站/urls.py中使用了名称空间-因此它适用于所有名称空间。请注意,OP中定义了登录注销-与a相同的名称