Django项目的密码重置循环出现异常错误

uqdfh47h  于 2023-08-08  发布在  Go
关注(0)|答案(1)|浏览(125)

我已经创建了一个登录系统,可以重置密码。
当我点击“忘记密码?”时,请求工作完美。“我收到了重置链接,但当我点击电子邮件中的链接时,我收到了错误消息。“异常类型:TypeError at /accounts/reset_password_validate/MzM/brm 129 - 52 c795 f84 dc 89 b21234642 b2166 f5 c4 f/异常值:reset_password()得到意外的关键字参数'uidb 64'“x1c 0d1x
我一直认为URL路径不是路由,但我已经更改了两次,我得到了相同的错误消息。URL.py

from django.urls import path
from . import views

urlpatterns = [
    # Other URL patterns...
    path('registerUser/', views.registerUser, name='registerUser'),
    path('registerBusiness/', views.registerBusiness, name='registerBusiness'),
    # Update the URL pattern to use the login view

    #path('accounts/login/', views.login, name='login'),
    path('login/', views.user_login, name='login'),
    path('logout/', views.logout, name='logout'),
    path('myAccount/', views.myAccount, name='myAccount'),
    path('custDash/', views.custDash, name='custDash'), 
    path('bussDash/', views.bussDash, name='bussDash'),  

    path ('activate/<uidb64>/<token>/', views.activate, name='activate'),

    path('forgot_password/', views.forgot_password, name='forgot_password'),
    path('reset_password/', views.reset_password, name='reset_password'),
    path('reset_password_validate/<str:uidb64>/<str:token>/', views.reset_password, name='reset_password_validate'),

]

字符串
views.py

from django.shortcuts import render , redirect
from django.contrib.auth.hashers import make_password

from business.forms import BussForm
from .forms import UserForm
from .models import User, userProfile
from django.contrib import messages, auth
from django.contrib.auth import authenticate, get_user_model, login as auth_login
from .utils import detectUser, send_verification_email
from django.contrib.auth.decorators import login_required, user_passes_test
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.contrib.auth.tokens import default_token_generator

from django.core.exceptions import PermissionDenied

# Create your views here.

#restrict user access to wrong dashbaords
def check_role_buss(user):
    if user.role == 1:
        return True
    else:
        raise PermissionDenied
    
def check_role_customer(user):
    if user.role == 2:
        return True
    else:
        raise PermissionDenied

# create user with form from website
def registerUser(request):
    context = {}
    if request.user.is_authenticated:
        messages.warning(request, 'You have already logged into your account')
        return redirect('dashboard')

    elif request.method == 'POST':
        form = UserForm(request.POST)
        if form.is_valid():
            # password = form.cleaned_data['password']
            # user = form.save(commit=False)  # Create a User instance without saving to the database
            # user.set_password(password)
            # user.role = User.CUSTOMER  # Assign the role to the user
            # user.save()  # Save the user to the database

            #create user from create usert method
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            #phone_Number = form.cleaned_data['phone_Number']
            password = form.cleaned_data['password']
            user = User.objects.create_user(first_name = first_name, last_name = last_name, username = username, email = email, password = password)
            user.role = User.CUSTOMER  # Assign the role to the user
            user.save()

            #send email verifcation
            send_verification_email(request, user)

            messages.success(request, 'Your accounts has been registered!')
            return redirect('registerUser')
        else:
            print('invalid entry')
            print(form.errors)
        
    else:
        form = UserForm()

    context['form'] = form
    return render(request, 'accounts/registerUser.html', context)

def registerBusiness(request):
    if request.user.is_authenticated:
        messages.warning(request, 'You have already logged into your account')
        return redirect('dashboard')
    elif request.method =='POST':
        #store data and creare the business
        form = UserForm(request.POST)
        b_form = BussForm(request.POST)
        if form.is_valid() and b_form.is_valid:
            first_name = form.cleaned_data['first_name']
            last_name = form.cleaned_data['last_name']
            username = form.cleaned_data['username']
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']
            user = User.objects.create_user(first_name = first_name, last_name = last_name, username = username, email = email, password = password)
            user.role = User.BUSINESS
            user.save()
            business = b_form.save(commit=False)
            business.user = user
            user_profile = userProfile.objects.get(user=user)
            business.user_profile = user_profile 
            business.save()

            #send verifcation email business 
            mail_subject = 'Please activate your account'
            email_template = 'accounts\emails\account_verif_email.html'
            send_verification_email(request,user, mail_subject, email_template)

            messages.success(request, 'Your business has been saved sucesfully and is now under approval')
            return redirect('registerBusiness')
        else: 
            print('invalid form')
            print(form.errors)
    else:
        form = UserForm()
        b_form = BussForm()

    context = {
        'form': form,
        'b_form': b_form,

    }
    return render (request, 'accounts/registerBusiness.html', context)

def activate(request, uidb64, token ):
    #activate user email from token vertifcation(setting =True)
    try:
        uid = urlsafe_base64_decode(uidb64).decode()
        user = User._default_manager.get(pk=uid)
    except(TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and default_token_generator.check_token(user, token):
        user.is_active = True
        user.save()
        messages.success(request, 'Your account has been sucessfully verifed and is now active! Thank you for your interest')
        return redirect ('myAccount')
    else:
        messages .error(request, 'invalid link, please try again')
        return redirect ('myAccount')
    

# manage form details for login, logour and loop incase of error or mistype of passowrd

#manages the pages based on the user roles and permissions for logging in
def user_login(request):
    if request.user.is_authenticated:
        messages.warning(request, 'You have already logged into your account')
        return redirect('dashboard')
    elif request.method == 'POST':
        email = request.POST['email']
        password = request.POST['password']

        user = authenticate(request, email=email, password=password)

        if user is not None:
            auth_login(request, user)  # Rename the function call to auth_login
            messages.success(request, 'You have successfully logged in.')
            return redirect('myAccount')
        else:
            messages.error(request, 'Invalid login details. Please check and try again.')
            return redirect('login')

    return render(request, 'accounts/login.html')
# manage user accounts, reset password, login and register
def logout(request):
    auth.logout(request)
    messages.info(request, 'You have logged out succesfully')
    return redirect('login')

@login_required(login_url='login')
def myAccount(request):
    user = request.user
    redirectUrl = detectUser(user)
    return redirect(redirectUrl)

@login_required(login_url='login')
@user_passes_test(check_role_buss)
def bussDash(request):
    return render(request, 'accounts/bussDash.html')

@login_required(login_url='login')
@user_passes_test(check_role_customer)
def custDash(request):
    return render(request, 'accounts/custDash.html')

def forgot_password(request):
    if request.method == 'POST':
        email = request.POST['email']

        if User.objects.filter(email=email).exists():
            user = User.objects.get(email__exact=email)    

            #send reset password email, used verfication email function instead to make it more efficient
            mail_subject = 'Reset Password Link'
            email_template = 'accounts/emails/reset_password_email.html'
            send_verification_email(request, user, mail_subject, email_template)

            messages.success(request, 'You password reset link has been sent to the added email address')
            return redirect('login')
        else:
            messages.error(request, 'Account does not exist. Please recheck your email address')
            return redirect('forgot_password')

    return render(request, 'accounts/forgot_password.html')

def reset_password_validate(request, uidb64, token):
    User = get_user_model()
    try:
        uid = urlsafe_base64_decode(uidb64).decode()
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and default_token_generator.check_token(user, token):
        request.session['uid'] = uid
        messages.info(request, 'Please reset your password')
        return redirect('reset_password')
    else:
        messages.error(request, 'Invalid reset password link.')
        return redirect('myAccount')

def reset_password(request, uidb64, token):
    User = get_user_model()
    try:
        uid = urlsafe_base64_decode(uidb64).decode()
        user = User.objects.get(pk=uid)
    except (TypeError, ValueError, OverflowError, User.DoesNotExist):
        user = None

    if user is not None and default_token_generator.check_token(user, token):
        if request.method == 'POST':
            password = request.POST['password']
            user.set_password(password)
            user.save()
            messages.success(request, 'Your password has been reset successfully. Please log in with your new password.')
            return redirect('login')
        else:
            context = {
                'uidb64': uidb64,
                'token': token,
            }
            return render(request, 'accounts/reset_password.html', context)
    else:
        messages.error(request, 'Invalid reset password link.')
        return redirect('myAccount')

def reset_password(request):
    return render(request, 'accounts/emails/reset_password_email.html')


account_verf_email.html

{% autoescape off%}
<!-- security feature to prevent cross scripting -->

Hi {{user.first_name}}, 

Please verify your email address by clicking on the below link. 
http://{{ domain }}{% url 'activate' uidb64=uid token=token %}

{% endautoescape %}


我已经检查了路由和uid被称为我不知道为什么它不重置passowrd链接或重定向到reset_password URL。我从这里改编了答案:Django : No Reverse Match on password reset和这里:NoReverseMatch on password_Reset_confirm
我真的很感谢你的帮助,谢谢这么多!

pod7payv

pod7payv1#

在views.py中有两个名为reset_password的视图
您的reset_password_activate视图调用了以下视图:

return redirect('reset_password')

字符串
您的URLs.py将其解码为此路由(注意它被称为views.reset_password)

path('reset_password/', views.reset_password, name='reset_password'),


views.py中的第一个reset_password视图就是这个视图

def reset_password(request, uidb64, token):


(you可能是指底部的这一个)

def reset_password(request):


但是你的路径没有描述任何参数,所以根据错误,uidb 64是意外的。
最简单的修复方法是简单地重命名两个视图中的一个,并确保每个视图都指向/被指向正确。

相关问题