Password change form URL broken

100 Views Asked by At

I am making a portfolio/blog. I have created a edit profile page using django and bootstrap available tools. The change edit profile form comes with a hyperlink that says click this form to change your password. I have configured the URL to /members/password but when i click the link it redirects me to 1/password and the number it gives is based off the ID of the user. Can someone help me fix this link so that it redirects to the working URL path i defined instead of this preset URL that doesnt work?

Is my best solution here to somehow get rid of the included password link entirely and place my own link inside an anchor tag mapped to the correct URL?

to clarify, when i manually enter the path as defined in my URls it does indeed work/

actual error message 'Page not found (404) Request Method: GET Request URL: http://localhost:8000/1/password/'

views.py

from django.shortcuts import render
from django.views import generic
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.urls import reverse_lazy 
from django.contrib.auth import logout 
from .forms import SignUpForm, EditProfileForm


class UserRegisterView(generic.CreateView):
    form_class = SignUpForm
    template_name = 'registration/register.html'
    success_url = reverse_lazy('login')



class UserEditView(generic.UpdateView):
    form_class = EditProfileForm
    template_name = 'registration/edit_profile.html'
    success_url = reverse_lazy('home')

    def get_object(self):
        return self.request.user 

urls.py

from .views import UserRegisterView, UserEditView
from django.urls import path
from django.contrib.auth import views as auth_views

urlpatterns = [
    path('register/', UserRegisterView.as_view(), name='register'),
    path('edit_profile/', UserEditView.as_view(), name='edit_profile'),
    path('password/', auth_views.PasswordChangeView.as_view()),
]

forms.py

from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from django import forms 

class SignUpForm(UserCreationForm):
    email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}))
    first_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
    last_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))

    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
    

    def __init__(self, *args, **kwargs):
        super(SignUpForm, self).__init__(*args, **kwargs)

        self.fields['username'].widget.attrs['class'] = 'form-control'
        self.fields['password1'].widget.attrs['class'] = 'form-control'
        self.fields['password2'].widget.attrs['class'] = 'form-control'
        


class EditProfileForm(UserChangeForm):
    email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}))
    first_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
    last_name = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
    username = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'form-control'}))
  




    class Meta:
        model = User
        fields = ('username', 'first_name', 'last_name', 'email')

edit: ive just had the thought to open that page and view source to look where that password link is even coming from and it looks like its the default django HTML for the form somehow, but im not sure where i can change this in the code in my project? the anchor tag in the HTML definitely has the wrong URL. I could easily add my own link that worked in addition to this but it wouldnt look so good to have one working and one broken link thats being unused.

0

There are 0 best solutions below