Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

django user form

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


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

    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']
        
        
### in views.py ###
from django.contrib.auth import login as auth_login
from .forms import RegisterForm

def sign_up(request):
    if request.method == 'POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save()
            auth_login(request, user)
    else:
        form = RegisterForm()
    
    return render(request, 'sign_up.html', {'form': form})
  
### add the following line in settings.py ###
LOGIN_REDIRECT_URL = "/home" 
### /home is the page user will be redirected to after login
Comment

PREVIOUS NEXT
Code Example
Python :: add text to plot python 
Python :: python add month datetime 
Python :: pandas convert index to column 
Python :: pandas read_csv ignore unnamed columns 
Python :: find different values from two lists python 
Python :: import xgboost 
Python :: how ot split a string every fourth eter 
Python :: print json python 
Python :: python file size 
Python :: convert json to x-www-form-urlencoded pyhon 
Python :: inspectdb django 
Python :: How to get random int between two numbers python 
Python :: python join array of ints 
Python :: run django app locally 
Python :: python pyodbc install 
Python :: each line in a text file into a list in Python 
Python :: pandas drop row by condition 
Python :: python pil image flip 
Python :: python function to print random number 
Python :: pandas change dtype to string 
Python :: tkinter how to disable window resizing 
Python :: pandas.core.indexes.base.index to list 
Python :: hbox(children=(floatprogress(value= 
Python :: get first of current month python 
Python :: timedelta year python 
Python :: matplotlib title 
Python :: pandas read csv with index 
Python :: python create map with coordinates 
Python :: array for each in python 
Python :: save matplotlib figure with base64 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =