Using the Django authentication system - Problem rendering password_change_form.html template in template/registration folder

136 views
Skip to first unread message

Teboho Bereng

unread,
Nov 11, 2019, 8:47:48 AM11/11/19
to django...@googlegroups.com
  Hello

I'm working on a project to have Django Authentication with signup, login, Logout, password_change and password_change_done, and password_reset functionality enabled for a particular website.I have to mention that I'm not very experienced as a developer.


The  signup, login, logout work fine and can render the signup and login html templates successfully as well which are the only two that require html templates but not the logout of course.  However, the password_change, (I would like to focus on password_change for now, much as the problem applies to password_done template and password_reset as well so what I do with password_change and get it to work should work with the others as well) cannot render the password_change_form.html but directs to this Django Admin password change default form:

image.png

I was following Votro Freitas videos found on   https://www.youtube.com/channel/UCS3lgmo8xYcOGTn_fv2mLgw when I was coding these functionalities and I could not get the html templates to load. Please look at the code in my files below and help me find out why this is happening as I did exactly as Vitor did in his videos and his html files can render successfully while mine do not. I only have login, logout and password_change in my base.html, please do note that so I need to get password_change to work first and can then move to focus on the others.

Below is code in my html files and .py files, and they are as follows:

views.py:
from django.shortcuts import render, redirect
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

def home(request):
    count = User.objects.count()
    return render(request, 'home.html',{
        'count': count
    })

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('home')
    else:      
        form = UserCreationForm()
    return render(request,'registration/signup.html',{
        'form':form
    })

Below is the base.html in the templates folder:
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    
    <title>{% block  title %}{% endblock %}</title>
</head>
<body>
    <h1>My Site</h1>
   <a href="{% url 'home' %}">Home</a> /
   {% if user.is_authenticated %}
   <a href="{% url 'password_change' %}">Change password</a> /
   <a href="{% url 'logout' %}">Log out</a>
   <strong>You are logged in as {{ user.username }}</strong> 
   {% else %}
   <a href="{% url 'signup' %}">Sign up</a> /
   <a href="{% url 'login' %}">Log in</a>
   {% endif %}
    <hr>
    {% block content %}
    {% endblock %}
</body>
</html>


the URLs config file urls.py:

from django.contrib import admin
from django.urls import path, include
from mysite.core import views

urlpatterns = [
    path('',views.home, name='home'),
    path('signup',views.signup, name='signup'),
    path('accounts/', include('django.contrib.auth.urls')),
    path('admin/', admin.site.urls),
]


signup.html in the templates/registration folder:
{% extends 'base.html' %}

{% block content %}
<h2>Sign Up</h2>
<form method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit"> Sign Up</button>
</form>
{% endblock %}


login.html in the templates/registration:
{% extends 'base.html' %}

{% block content %}
    <h2>Log in</h2>
<form method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Log in</button>
</form>
{% endblock %}


password_change_form in templates/registration folder:
{% extends 'base.html' %}

{% block content %}
    <h2>Change password</h2>
<form method="post">
{% csrf_token %}
{{form.as_p}}
<button type="submit">Change password</button>
</form>
{% endblock %}


password_change_done in templates/registration and also does not render the html template:

{% extends 'base.html' %}

{% block content %}

<p>Change password</p>
<p style="color:green;">Password changed with success!</p>

{% endblock %}

I hope to hear from you and thank you for your help in advance. I'm glad to join this community.

Regards
Dennis.



















Reply all
Reply to author
Forward
0 new messages