gpt4 book ai didi

python - 引发 NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch : Reverse for 'login

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:03 26 4
gpt4 key购买 nike

我正在尝试添加我单独制作的用户应用程序,然后尝试将其添加到我的博客中。但问题是每次我尝试添加这个东西都会搞砸,就像我现在遇到的这个错误一样。

raise NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch: Reverse for 'login' not found. 'login' is not a valid view function or pattern name.

so far i have done this

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')

最佳答案

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

此网址已被注释掉,并且您没有任何其他 name="login"

关于python - 引发 NoReverseMatch(msg) django.urls.exceptions.NoReverseMatch : Reverse for 'login,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55193365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com