Register page redirect to home page or My page is not loading

The below codes are the codes i used to create my register form. When i try to go to 127.0.0.1:8000/register/, this was redirecting to my home page i mean it render my index.html page i cant find find any mistakes in my code. And, i dont know why this is redirecting to my home page. Even i try to new virtual env, copy my files to my friend laptop, reinstalled my browser but nothing works. Its been a weak still i can’t find what is happening here.

urls.py

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

views.py

def register(request):
    if request.method == 'POST':
        form = UserRegisterForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            email = form.cleaned_data.get('email')
            htmly = get_template('registration/email.html')
            d = { 'username': username }
            subject, from_email, to = 'Welcome', 'myemail@gmail.com', email
            html_content = htmly.render(d)
            msg = EmailMultiAlternatives(subject, html_content, from_email, [to])
            msg.attach_alternative(html_content, "text/html")
            msg.send()
            messages.success(request, f'Your account has been created ! You are now able to log in')
            return redirect('login')
    else:
        form = UserRegisterForm(request.POST)
    return render(request, 'registration/register.html', {'form':form})

form.py

class UserRegisterForm(UserCreationForm):

    email = forms.EmailField()

    class Meta:
        model = User
        fields = {'username', 'email', 'password1', 'password2'}

    def save(self, commit=True):
        user = super(UserRegisterForm, self).save(commit=False)
        user.name = self.cleaned_data['name']
        user.email = self.cleaned_data['email']
        if commit:
            user.save()
        return user

register.html

{% extends "index.html" %}
{% load crispy_forms_tags %}
{% block start %}

<div class="container">
    <form method="post" enctype="multipart/form-data">
        {% csrf_token %}
        <table>
        {{ form }}
        </table>
        <button type="submit">Submit</button>
    </form>
    <sub><a href="{% url 'login' %}">Login to your Account</a></sub>
</div>

{% endblock start %}

This was the response I get when I try to go to the page.

[29/Jul/2021 07:43:33] "GET /register/ HTTP/1.1" 200 1317

Hi @GoAmeer030,

I can’t quite tell what you’re expecting to happen and what is happening. Can you elaborate on those for me please?

@CodenameTim I edited it bro. check it and help me with it bro.

We need some more clarification here.

Are you saying that, if you look at the page actually rendered in your browser (using either your browser’s developer tools or “view source”), you don’t see any of the parts of that HTML I quoted above? That you don’t see at least the <div class="container"> tag?

If not, we’ll need to see your index.html template.

(Also, I’ll point out that you have the lines of code:

That else clause is for the case when the request.method is not “POST”. Since the request is not a POST, request.POST is going to be null. There’s no value (and is potentially confusing) to be passing request.POST in to the constructor of your form.


This is the page loaded from 127.0.0.1:8000/register/(“This Url”)

Sorry guys i cant upload 127.0.0.1:8000/ page screenshot because i am a new user. As you guys can see that 127.0.0.1:8000/register/ url is loading my index.html page. I made register.html and i assigned it to 127.0.0.1:8000/register/. but instead of register.html, index.html is loading (register.html is on the question section). I guess @KenWhitesell and @CodenameTim you guys will understand it (Please see the url bar then you guys will understand my problem).

@KenWhitesell The code is my index.html
index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    {% load static %}
    <title>Document</title>
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Montserrat:wght@100&display=swap');
    </style>
    <link rel="stylesheet" href="{% static 'style.css' %}">
</head>
<body>
    <nav class="navbar">
        <ul>
            <div>
                <li><a href="#">Danker Man</a></li>
            </div>
            {% if user.is_authenticated %}
            <div>
                <li><a href="#">Features</a></li>
                <li><a href="#">Disclaimer</a></li>
                <li><a href="#">PrivacyPolicy</a></li>
                <li><a href="#">Terms&Conditions</a></li>
                <li><a href="{% url 'bot' %}">Bot</a></li>
                <li><a href="{% url 'edit_profile' %}">Edit Profile</a></li>
            </div>
            {% else %}
            <div>
                <li><a href="#">Features</a></li>
                <li><a href="#">Disclaimer</a></li>
                <li><a href="#">PrivacyPolicy</a></li>
                <li><a href="#">Terms&Conditions</a></li>
                <li><a href="{% url 'login' %}">Login</a></li>
            </div>
            {% endif %}
        </ul>
    </nav>
    {% block content %}
    <section class="content-container">
        <div class="content-box">
            <div class="content">
                <h1>Danker Man</h1>
                <p>This is a Discord Bot!</p>
            </div>
        </div>
    </section>
    {% endblock content %}
    <section class="footer">
        <div id="cr">Copyrighted &copy by Danker Man</div>
    </section>
    <script src="{% static 'app.js' %}"></script>
</body>
</html>

(“If you guys still cant understand me . Please send me a reply and i will make screen recording and attach that link in my next reply”)

For your register.html, you posted:

But I don’t see a {% block start %} {% endblock start %} pair in your index.html. The template you extend must provide a spot for the extension’s content to be placed.

@KenWhitesell Yes bro i made a silly mistake. Here after I more focused on my codes. Thanks a lot and keep your great job on.