Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use django PasswordResetView functionality in my own view

I want to use django's default password reset view "PasswordResetView" which let's the user reset his password when he forgets it in a template that already has a view that i built on my own, after looking at the tutorials and the questions i found how to use it only on a different template that is made only for the password reset, but i don't want the user to go to a different page just to change his password when he forgets it, i want to make it in a bootstrap modal in the home page.

here is my home view that i want to add PasswordResetView functionality to it:

def home(request):
    
    user = request.user
    signin_form = SigninForm()
    signup_form = SignupForm()




    if request.method == "POST":
        if 'signin_form' in request.POST:
            signin_form = SigninForm(request.POST)
            if signin_form.is_valid():
                    email = request.POST['email']
                    password = request.POST['password']
                    user = authenticate(email=email, password=password)
                    if user:
                        login(request, user)
                    elif user is None:
                        messages.error(request, 'ُEmail or password is incorrect')



        if 'signup_form' in request.POST:
            signup_form = SignupForm(request.POST)
            if signup_form.is_valid():
                signup_form.save()
                full_name = signup_form.cleaned_data.get('full_name')
                email = signup_form.cleaned_data.get('email')
                raw_password = signup_form.cleaned_data.get('password1')
                account = authenticate(email=email, password=raw_password)
                login(request, account)
                
   
    context = {'signin_form': signin_form,'signup_form': signup_form}

    return render(request, 'main/home.html', context)

PS: i tried copy pasting the source code of that view (PasswordResetView) from django's source code in my view but i found some errors because it's a class based view, so if you find this the proper way, guide me to do it
or if i can't merge them somehow how to create a custom one

this is what i found in the other answers which lets you use it in a certain template that has only that view (PasswordResetView) which is not what i want:


from django.contrib.auth import views as auth_views

     path('password_reset/', auth_views.PasswordResetView.as_view(template_name="myapp/mytemplate.html",form_class=mypasswordresetform),name="reset_password"),
like image 936
Mohcen CH Avatar asked Oct 06 '20 19:10

Mohcen CH


People also ask

How to use logout in Django?

To log out a user who has been logged in via django.contrib.auth.login() , use django.contrib.auth.logout() within your view. It takes an HttpRequest object and has no return value. Example: from django.contrib.auth import logout def logout_view(request): logout(request) # Redirect to a success page.


1 Answers

I'll give you a simple approach to having a password reset feature on your django application. Before having any code, let me give a brief exlanation of the process. What you want to do is get a user to input their email, check if there is any user with that email, then if there is one, send an email to that address with a uniquely generated link.

From this link, you should be able to extract the user object which you need to change password. An example would be to use django's signing module. This link will simply need to redirect the user to a template where there is a form with 2 fields i.e. New Password and Verify Password.

Django's generic views come with this functionality out-of-the-box if you are using Django's authentication module, but you aren't forced to use it, but its best to do so.

Here I'll only show you how to collect the email address on the same view as you said you wanted.


def home(request):
    # ...your other code
    if request.method == 'post':
        if 'reset_password' in request.POST:
            email = request.POST.get("email", "")
            user_qs  = User.objects.filter(email=email)
            if not user_qs.exists():
                # send error message to user here
            else:
                user = user_qs.get()
                # send email with uniquely generated url here.

The other aspects of generating a URL and sending the mail, I believe you can research these separately. But I hope you now have an idea of where and what to search.

like image 94
Redgren Grumbholdt Avatar answered Sep 24 '22 07:09

Redgren Grumbholdt