Issue with Registration: User Data Not Saving to Database (Django)

22 Views Asked by At

I've developed registration functionality for my web application using a combination of form elements, a view, a registration template, and JavaScript modals. However, despite completing the registration process, the user data is not being saved to the database as expected.

Details:

Form: I have implemented a form for user registration. View: A corresponding view function handles the registration process. Template: I've created a registration template for rendering the registration form. JavaScript Modals: Modal dialogs are used for user interaction during the registration process.

Problem Statement: When I submit the registration form, the user data does not get stored in the database. I've verified that the form submission is successful, but somehow the data isn't persisting.

Expectation: Upon successful submission of the registration form, the user data should be saved to the database for future reference and authentication purposes.

Request for Assistance: I'm seeking guidance on what could be causing this issue and how to troubleshoot it effectively. Any insights, suggestions, or potential solutions would be greatly appreciated. Thank you!

My code:

Base code, containing JavaScript modals:

{% load static %}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="{% static 'styles.css' %}">
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>

    <!-- JavaScript for modals -->
    <script>
        // Function to show login modal
        // Function to show login modal
        function showLoginModal() {
            document.getElementById('login-modal').style.display = 'block';
            document.getElementById('logout-modal').style.display = 'none';  // Hide logout modal
            document.getElementById('main-content').style.filter = 'blur(5px)';
        }

        // Function to show logout modal
        function showLogoutModal() {
            document.getElementById('logout-modal').style.display = 'block';
            document.getElementById('login-modal').style.display = 'none';  // Hide logout modal
            document.getElementById('main-content').style.filter = 'blur(5px)';
        }

        // Function to show register modal
        function showRegisterModal() {
            document.getElementById('register-modal').style.display = 'block';
            document.getElementById('main-content').style.filter = 'blur(5px)';
        }

        // Function to hide modals
        function hideModals() {
            document.getElementById('login-modal').style.display = 'none';
            document.getElementById('register-modal').style.display = 'none';
            document.getElementById('logout-modal').style.display = 'none';
            document.getElementById('main-content').style.filter = 'none'; // Remove blur
        }

        // Close modals when clicking outside of them
        window.onclick = function(event) {
            var loginModal = document.getElementById('login-modal');
            var logoutModal = document.getElementById('logout-modal');
            var registerModal = document.getElementById('register-modal');
            if (event.target == loginModal || event.target == registerModal || event.target == logoutModal) {
                hideModals();
            }
        }
    </script>

    <style>
        .navbar-custom {
            height: 50px;
        }

        /* Style for main content (to blur when modal is shown) */
        #main-content {
            transition: filter 0.3s; /* Smooth transition for blur effect */
        }
    </style>
</head>
<body>

<!-- Navbar -->
<nav class="main-top-navbar navbar navbar-dark bg-dark fixed-top navbar-custom">
    <div class="container">
        <a href="#" class="navbar-brand">Your Logo</a>
        
        <div class="main-top-navbar-elements navbar-text ml-auto">
            <a href="#" class="font-weight-bold" style="margin-right: 30px;">System |</a>
            <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;">Cards |</a>
            <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;">Temples |</a>
            <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;">Blog |</a>
            <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;">Shop |</a>
            <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;">Membership |</a>
            {% if not user.is_authenticated %}
                <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;" onclick="showLoginModal()">Login |</a>
                <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;" onclick="showRegisterModal()">Register |</a>
            {% else %}
                <a href="#" class="text-light font-weight-bold" style="margin-right: 30px;" onclick="showLogoutModal()">Logout |</a>
            {% endif %}
        </div>
    </div>
</nav>

<!-- Main content of your website -->
<div id="main-content">
    <!-- Your website content here -->
    {% block body %}
    
    {% endblock %}
</div>

<!-- Login Modal -->
<div id="login-modal" class="modal">
    <div class="modal-content">
        <!-- Include your login form here -->
        {% include 'registration/login.html' %}
    </div>
</div>

<!-- Logout Modal -->
<div id="logout-modal" class="modal">
    <div class="modal-content">
        <!-- Include your login form here -->
        {% include 'registration/logout.html' %}
    </div>
</div>


<!-- Register Modal -->
<div id="register-modal" class="modal">
    <div class="modal-content">
        <!-- Include your register form here -->
        {% include 'registration/register.html' %}
    </div>
</div>

<!-- Footer -->
<nav class="navbar navbar-dark bg-dark fixed-bottom">
    <div class="container">
        <span class="navbar-text text-light font-weight-bold mx-auto">
            Follow the truth!
        </span>
    </div>
</nav>

</body>
</html>

Forms code, containing registration form:

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

class UserRegistrationForm(UserCreationForm):

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

View code, containing registration view

def register_view(request):

    if request.method == 'POST':
        form = UserRegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect("index")  
    else:
        form = UserRegistrationForm()

    print(form.errors)

    context = {'register_form': form}
    return render(request, 'registration/register.html', context)

Template code, containing the register.html template.

<form class="form" method="POST">
    {% csrf_token %}
    <label class="label" for="email">Email:</label>
    <input class="input" type="text" id="email" name="email">
    <label class="label" for="username">Username:</label>
    <input class="input" type="text" id="username" name="username">
    <label class="label" for="password">Password:</label>
    <input class="input" type="password" id="password" name="password">
    <label class="label" for="confirm_password">Confirm Password:</label>
    <input class="input" type="password" id="confirm_password" name="password2">
    <button class="button" type="submit">Register</button>
</form>
1

There are 1 best solutions below

2
Mohamed ElKalioby On

The form is not valid because you didn't provide a value for password1. You need to fix your HTML template as follows

<label class="label" for="password">Password:</label>
<input class="input" type="password" id="password" name="password1">