Home > Software engineering >  Django signup form doesn't submit data or save in database or valid and invalid errors working
Django signup form doesn't submit data or save in database or valid and invalid errors working

Time:07-17

$ I was trying to submit this following codes , it never submit or saved in database or give any reaction , even the warnings or (valid and invalid of bootsrap5.2 in not working) , i need support, thank you alot

$ this is code in the (views.py),

from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login as signin
from django.contrib.auth import authenticate
# This can replace the 3 uppers
from .forms import SignUpForm
def signup(request):  
form = SignUpForm()
if request.method == 'POST' and 'btnsignup2' in request.POST:
    form = SignUpForm(request.POST)
    if form.is_valid(): 
        user = form.save()
        signin(request, user)
        return redirect('index')
    else:
        form = SignUpForm()

context = {                
    'basic': 
        {'main': 'Project',
            },
    'form': form
}
return render(request, 'accounts/signup-dj.html', context)

$ this is code in the (forms.py)

from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login as signin
from django.contrib.auth import authenticate
from django.contrib.auth.models import User

class SignUpForm(UserCreationForm):
email = forms.EmailField(max_length=250, required=True, widget=forms.EmailInput())
class Meta:
    model = User 
    fields = ['first_name', 'last_name', 'email', 'username', 'password1', 'password2']

$ this is (urls.py)

from django.urls import path
from . import views

urlpatterns = [
path('login', views.login, name="login"),
path('signout', views.signout, name="signout"),
path('signup', views.signup, name="signup"),
]

$ this is code in the (HTML)

{% extends 'base.html' %}
{% load static %} 
{% block title %}Sign Up{% endblock %}<!-- | Page-title-->
{% block content %}
<div >
<div >
    <h1>Sign Up</h1>
    <br>
    {% include 'parts/alerts.html' %}
    <form  metho="POST" action="">
        {% csrf_token %} 
        {% include 'includes/form.html' %}


        <button type="submit" name="btnsignup2" >
            Create New User</button>
        <p >
            You Already Have Account at {{basic.main}} ? <a href="{% url 'login'
          %}">Login</a>
        </p>
    </form>
    </div>
    </div>
{% endblock %} 

CodePudding user response:

Just looking at the HTML I can see that here:

<form  metho="POST" action="">

you have a typo in metho="POST", it should be method="POST", and assign the url of the endpoint to action, like action="/signup".

  • Related