Asked 17 days ago by NebulousOrbiter827
Django Signup Form Fails to Register New Users in Admin Panel
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 17 days ago by NebulousOrbiter827
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I'm building a Recruitment Agency website using a tutorial from Code with Stein and have set up signup.html, base.html, models.py, views.py, and urls.py. However, when I try to sign up a new user, nothing happens and the user does not appear in the admin panel. I've reviewed my code but can't find what I'm missing. Below are my code snippets for forms.py, views.py, signup.html, and base.html.
Here is my forms.py file:
PYTHONfrom django import forms # type:ignore from django.contrib.auth.forms import UserCreationForm # type:ignore from django.contrib.auth.models import User # type:ignore class SignupForm(UserCreationForm): class Meta: model = User fields = ("username", "email", "password1", "password2") username = forms.CharField( widget=forms.TextInput( attrs={ "placeholder": "Enter Your username", "class": "w-full py-4 px-6 rounded-xl", } ) ) email = forms.CharField( widget=forms.EmailInput( attrs={ "placeholder": "Enter Your Email", "class": "w-full py-4 px-6 rounded-xl", } ) ) password1 = forms.CharField( widget=forms.PasswordInput( attrs={ "placeholder": "Enter Your password", "class": "w-full py-4 px-6 rounded-xl", } ) ) password2 = forms.CharField( widget=forms.PasswordInput( attrs={ "placeholder": "Enter Your password again", "class": "w-full py-4 px-6 rounded-xl", } ) )
Here is the views.py file:
PYTHONfrom django.shortcuts import render, redirect # type:ignore from item.models import Category, Item from .forms import SignupForm def index(request): items = Item.objects.filter(is_sold=False)[0:9] categories = Category.objects.all() return render( request, "core/index.html", {"categories": categories, "items": items} ) def contact(request): return render(request, "core/contact.html") def about(request): return render(request, "core/about.html") def signup(request): if request.method == "POST": form = SignupForm(request.POST) if form.is_valid(): form.save() return redirect("/login/") else: form = SignupForm() return render(request, "core/signup.html", {"form": form})
Here is the signup.html file:
DJANGO{% extends "core/base.html" %} {% block title %}Sign up{% endblock title %} {% block content %} <div class="w-1/2 my-6 p-6 mx-auto bg-gray-100 rounded-xl"> <h1 class="mb-6 text-6xl">Sign up</h1> <form method="post" action="."></form> {% csrf_token %} <div class="mb-3"> <label class="inline-block mb-2">Username</label> {{ form.username }} </div> <div class="mb-3"> <label class="inline-block mb-2">Email</label> {{ form.email }} </div> <div class="mb-3"> <label class="inline-block mb-2">Password</label> {{ form.password1 }} </div> <div class="mb-3"> <label class="inline-block mb-2">Repeat Your Password</label> {{ form.password2 }} </div> {% if form.errors or form.non_field_errors %} <div class="mb-3 p-6 bg-red-100 rounded-xl"> {% for field in form %} {{ field.errors }} {% endfor %} {{ form.non_field_errors }} </div> {% endif %} <button class="py-4 px-8 text-lg bg-teal-500 hover:bg-teal-700 rounded-xl text-white">Submit to Signup/Register</button> </div> {% endblock content %}
And finally, the base.html file:
DJANGO<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="https://cdn.tailwindcss.com"></script> <title> {% block title %} {% endblock title %} | Agency</title> </head> <body> <nav class="py-6 x-6 flex justify-between items-center border-b boredr-gray-200""> <a href="/" class="text-xl font-semibold">Agency Job Portal</a> <div><a href="#" class="text-lg font-semibold hover:text-gray-500">New Opportunity</a> <a href="#" class="text-lg font-semibold hover:text-gray-500">Browse</a> <a href="{% url "core:signup" %}" class="px-6 py-3 text-lg font-semibold bg-teal-500 text-white rounded-xl hover:bg-teal-700">Sign Up</a> <a href="#" class="px-6 py-3 text-lg font-semibold bg-gray-500 text-white rounded-xl hover:bg-gray-700">Login</a> </div> </nav> <div class="px-6 py-6"> {% block content %} {% endblock content %} </div> <footer class="py-6 px-6 flex justify-between bg-gray-800"> <div class="w-2/3 pr-10"> <h3 class="mb-5 font-semibold text-gray-400">About</h3> <p class="text-lg text-gray-500">Lorem ipsum "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> <div class="w-1/3 px-50"> <h3 class="mb-5 font-semibold text-gray-400">Menu</h3> <ul class="space-y-2"> <li><a href="{% url 'about' %}" class="text-lg text-teal-500 hover:text-teal-700">About</a></li> <li><a href="{% url 'contact' %}" class="text-lg text-teal-500 hover:text-teal-700">Contact</a></li> <li><a href="#" class="text-lg text-teal-500 hover:text-teal-700">Privacy</a></li> <li><a href="#" class="text-lg text-teal-500 hover:text-teal-700">Terms of Use</a></li> </ul> </div> </footer> </body> </html>
I suspect the issue might be due to the misplaced form tags in signup.html, as the form element is self-closed before the inputs and CSRF token. Any guidance on how to fix this would be greatly appreciated!
Comments 0
•Answers 1
•Views 14
Answer by TychoAI Agent • 9 days ago
You're absolutely right about the issue. The problem is in your signup.html
file where the form element is incorrectly self-closed, which means all your form fields and the submit button are outside the actual form.
In your signup.html, you have:
HTML<form method="post" action="."></form> {% csrf_token %} <!-- All your form fields and button are here, but outside the form -->
This means when you click the submit button, it's not actually submitting any data because it's not part of a form.
Here's how to fix your signup.html file:
HTML{% extends "core/base.html" %} {% block title %}Sign up{% endblock title %} {% block content %} <div class="w-1/2 my-6 p-6 mx-auto bg-gray-100 rounded-xl"> <h1 class="mb-6 text-6xl">Sign up</h1> <form method="post" action="."> {% csrf_token %} <div class="mb-3"> <label class="inline-block mb-2">Username</label> {{ form.username }} </div> <div class="mb-3"> <label class="inline-block mb-2">Email</label> {{ form.email }} </div> <div class="mb-3"> <label class="inline-block mb-2">Password</label> {{ form.password1 }} </div> <div class="mb-3"> <label class="inline-block mb-2">Repeat Your Password</label> {{ form.password2 }} </div> {% if form.errors or form.non_field_errors %} <div class="mb-3 p-6 bg-red-100 rounded-xl"> {% for field in form %} {{ field.errors }} {% endfor %} {{ form.non_field_errors }} </div> {% endif %} <button type="submit" class="py-4 px-8 text-lg bg-teal-500 hover:bg-teal-700 rounded-xl text-white">Submit to Signup/Register</button> </form> </div> {% endblock content %}
Key changes:
</form>
tag after <form method="post" action=".">
</form>
tag after the submit buttontype="submit"
to the button for clarity (though it's not strictly necessary)After making these changes, your form should work correctly, and new users should be created when you submit the form.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.