Last active
June 25, 2022 17:31
-
-
Save aliceridgway/305d778ef4af5f28ecc866b92a360de4 to your computer and use it in GitHub Desktop.
Django: How to automatically log in users after registration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import include, path | |
from . import views | |
urlpatterns = [ | |
path('signup', views.SignUp.as_view(), name='signup'), | |
] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.urls import reverse_lazy | |
from django.views.generic.edit import CreateView | |
from django.contrib.auth import authenticate, login | |
from .models import User | |
from .admin import UserCreationForm | |
class SignUp(CreateView): | |
model = User | |
form_class = UserCreationForm | |
template_name = 'accounts/signup.html' | |
success_url = reverse_lazy('create_profile') | |
def form_valid(self, form): | |
valid = super(SignUp, self).form_valid(form) | |
email, password = form.cleaned_data.get('email'), form.cleaned_data.get('password1') | |
user = authenticate(email=email, password=password) | |
login(self.request, user) | |
return valid |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment