user registration django code example
Example 1: signup view
from django.contrib.auth import authenticate
class SignUpView(CreateView):
form_class = UserRegistrationForm
success_url = reverse_lazy('home')
template_name = 'registration/signup.html'
def form_valid(self, form):
view = super(SignUpView, self).form_valid(form)
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(self.request, user)
return view
Example 2: django create user
from django.contrib.auth.models import User
user = User.objects.create_user(value['USERNAME'], value['EMAIL'], value['PASSWORD'])
user.first_name = value['NAME']
user.last_name = value['SURNAME']
user.save()