遇到错误:找不到与“大学”相反的位置。 “学院”不是有效的视图函数或模式名称

时间:2019-12-22 13:00:51

标签: django django-models django-rest-framework django-forms django-views

urls.py:

from django.urls import path
from django.contrib.auth import views as auth_views
from . import views

app_name = 'accounts'

urlpatterns = [
    path('login/', auth_views.LoginView.as_view(template_name="accounts/login.html"),name='login'),
    path('logout/', auth_views.LogoutView.as_view(), name="logout"),
    path('college/', views.CollegeView.as_view(), name="college"),
    path('signup/', views.SignUp.as_view(), name="signup"),

]

views.py

from django.contrib.auth import login, logout
from django.urls import reverse_lazy
from django.views.generic import CreateView
from . import forms
from accounts.models import College
# Create your views here.

class CollegeView(CreateView):
    form_class = College
    fields = ['college']
    success_url = reverse_lazy("login")
    template_name = "accounts/college.html"

class SignUp(CreateView):
    form_class = forms.UserCreateForm
    success_url = reverse_lazy("college")
    template_name = "accounts/signup.html"

forms.py

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm
from django import forms

class UserCreateForm(UserCreationForm):
    class Meta:
        model = get_user_model()
        fields = ("username", "email", "password1", "password2")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["username"].label = "Display name"
        self.fields["email"].label = "Email address"

class CollegeForm(forms.Form):
    fields = ("college")

如果我尝试将成功网址重定向到登录或注销,那么它可以工作,但对于大学来说却不起作用

1 个答案:

答案 0 :(得分:1)

似乎您正在使用名称空间作为网址。

尝试根据 success_url 这样设置app_name

success_url = reverse_lazy("accounts:college")

或通过 include 方法

从根urls.py文件中删除名称空间。