Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR 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
Source by # #
 
PREVIOUS NEXT
Tagged: #django #user #form
ADD COMMENT
Topic
Name
2+9 =