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 :: django media root 
Python :: time.sleep() faster 
Python :: python google chrome 
Python :: keys in python 
Python :: corr pandas 
Python :: django objects.create() 
Python :: Permission denied in terminal for running python files 
Python :: get the name of a file using os 
Python :: where is tensorflow slim 
Python :: django radio button 
Python :: make python3 default in ubuntu 
Python :: circumference of circle 
Python :: python delete text in text file 
Python :: read excel into dataframe python 
Python :: python fillna with mode 
Python :: solve sympy 
Python :: python pandas apply function to one column 
Python :: Taking a list of strings as input, our matching function returns the count of the number of strings whose first and last chars of the string are the same. Also, only consider strings with length of 2 or more. python 
Python :: one-line for loop python 
Python :: how to make a rect in pygame 
Python :: pip tensorflow 
Python :: merge two series by index 
Python :: multiply each element in list python 
Python :: index in list 
Python :: change dataframe value by index 
Python :: drop all characters after a character in python 
Python :: Beautifulsoup - How to open images and download them 
Python :: program count the number of occurrences of a letter in a string python 
Python :: length of dataframe 
Python :: count number of spaces in string python 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =