Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Django Signup view

from django.contrib.auth import login, authenticate
from django.contrib.auth.forms import UserCreationForm
from django.shortcuts import render, redirect

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('home')
    else:
        form = UserCreationForm()
    return render(request, 'signup.html', {'form': form})
Comment

signup view django

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

class NewUserForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

    def save(self, commit=True):
        user = super(NewUserForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        if commit:
            user.save()
        return user
      
#views.py
from django.contrib import messages
from django.shortcuts import render, redirect

from .forms import NewUserForm

def sign_up_view(request):
    if request.method == 'POST':
        form = NewUserForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            messages.success(request, "Registration successful.")
            return redirect('inventory:login')
        messages.error(request, "Unsuccesful information. Invalid information")
    form = NewUserForm()
    return render(request, 'registration/signup.html', context={"register_form":form})
Comment

PREVIOUS NEXT
Code Example
Python :: remove outlier using IQR 
Python :: docker python 3.11 
Python :: TypeError: can only concatenate str (not "list") to str 
Python :: how to create a window in pygame 
Python :: break continue pass in python 
Python :: linux python virtual environment 
Python :: break 
Python :: python string caps lock 
Python :: find distance between two points in python 
Python :: draw canvas in python 
Python :: return key from value dictionary python 
Python :: how to check if a value is nan in python 
Python :: matplotlib set colorbar range 
Python :: list unpacking python 
Python :: jupyter notebook set password 
Python :: what is python 
Python :: delete function python 
Python :: run python from c# 
Python :: how to refresh page in flask 
Python :: show which columns in dataframe have NA 
Python :: Fun & learn with python turtle 
Python :: multiline comment 
Python :: re python 
Python :: abstract class in python 
Python :: best python programs 
Python :: How to get the Tkinter Label text 
Python :: csv to txt code pandas 
Python :: python array spread 
Python :: == in python 
Python :: python main template 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =