提高NoReverseMatch(msg)django.urls.exceptions.NoReverseMatch:为'login

时间:2019-03-16 04:08:11

标签: python django

我正在尝试添加我单独制作的用户应用,然后尝试将其添加到我的博客中。但是问题是每次我尝试添加此内容时,都会出现一些错误,就像我现在收到的此错误一样。

  

提高NoReverseMatch(msg)   django.urls.exceptions.NoReverseMatch:找不到“登录”的反向。 “登录”不是有效的视图函数或模式名称。

     

到目前为止,我已经做到了

     

blog / urls.py

from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from users.views import login_view, register_view, logout_view

# from django.contrib.auth import views as auth_views
# from users import views as user_views
from posts.views import index,postDetail, categoryDetail, blog, search

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', index, name="home"),
    path('blog/', blog, name="blog"),
    path('search/', search, name='search'),
    path('<slug>/', postDetail, name='post-detail'),
    path('category/<slug>/', categoryDetail, name='category-detail'),
    path('login/', login_view),
    path('logout/', logout_view),
    path('register/', register_view),
    # path('register/', user_views.register, name='register'),
    # path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),
    # path('logout/', auth_views.LogoutView.as_view(template_name='users/logout.html'), name='logout'),
    path('tinymce/', include('tinymce.urls'))
]
  

settings.py

LOGIN_REDIRECT_URL = 'home'
LOGIN_URL = 'login'
  

users / forms.py

    from django import forms
    from django.contrib.auth import (
        authenticate,
        get_user_model
    )
    User = get_user_model()

    class UserLoginForm(forms.Form):
        username = forms.CharField()
        password = forms.CharField(widget=forms.PasswordInput)

        def clean(self, *args, **kwargs):
            username = self.cleaned_data.get('username')
            password = self.cleaned_data.get('password')

            if username and password:
                user = authenticate(username = username, password=password)
                if not user: 
                    raise forms.ValidationError('This user does not exits')
                if not user.check_password(password):
                    raise forms.ValidationError('Incorrect Password')
                if not user.is_active:
                    raise forms.ValidationError('This user is not active')
            return super(UserLoginForm, self).clean(*args, **kwargs)

    class UserRegisterForm(forms.ModelForm):
        email = forms.EmailField(label='Email Address')
        email2 = forms.EmailField(label= 'Confirm Email')
        password = forms.CharField(widget=forms.PasswordInput)

        class Meta:
            model = User
            fields =[
                'username',
                'email',
                'email2',
                'password'
            ]

        def clean(self, *args, **kwargs):
            email = self.cleaned_data.get('email')
            email2 = self.cleaned_data.get('email2')
            if email != email2:
                raise forms.ValidationError('email must match')
            eamil_qs = User.objects.filter(email=email)
            if eamil_qs.exists():
                raise forms.ValidationError(
                    "This email is already being used"
                )
            return super(UserRegisterForm, self).clean(*args, **kwargs)
  

users / views.py

# Create your views here.
from django.shortcuts import render, redirect
from django.contrib.auth import (
    authenticate,
    get_user_model,
    login, 
    logout
)

from .forms import UserLoginForm, UserRegisterForm
# Create your views here.
def login_view(request):
    next = request.GET.get('next')
    form = UserLoginForm(request.POST or None)
    if form.is_valid():
        username = form.cleaned_data.get('username')
        password = form.cleaned_data.get('password')
        user = authenticate(username= username, password=password)
        login(request, user)
        if next: 
            return redirect(next)
        return redirect('/')
    context = {
        'form': form
    }
    return render(request, 'login.html', context)

def register_view(request):
    next = request.GET.get('next')
    form = UserRegisterForm(request.POST or None)
    if form.is_valid():
        user = form.save(commit=False)
        password = form.cleaned_data.get('password')
        user.set_password(password)
        user.save()
        new_user = authenticate(username= user.username, password=password)
        login(request, new_user)
        if next: 
            return redirect(next)
        return redirect('/')
    context = {
        'form': form
    }
    return render(request, 'signup.html', context)

def logout_view(request):
    logout(request)
    return redirect('/login')

2 个答案:

答案 0 :(得分:0)

# path('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login'),

此网址已被注释掉,您没有其他人使用name="login"

答案 1 :(得分:0)

您尚未给登录URL命名,

path('login/', login_view, name='login'),