Skip to content

Instantly share code, notes, and snippets.

@aliceridgway
Last active June 25, 2022 17:31
Show Gist options
  • Save aliceridgway/305d778ef4af5f28ecc866b92a360de4 to your computer and use it in GitHub Desktop.
Save aliceridgway/305d778ef4af5f28ecc866b92a360de4 to your computer and use it in GitHub Desktop.
Django: How to automatically log in users after registration
from django.urls import include, path
from . import views
urlpatterns = [
path('signup', views.SignUp.as_view(), name='signup'),
]
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