Answer the question
In order to leave comments, you need to log in
How to add fields from another model to a Django registration form?
Hey!
I use this design (code below) to register users using the standard Users model. There was a need to add several fields from another model (UserInfo) to the registration form. Tell me how to do it better?
Thanks in advance!
def register(request):
""" User registration """
if auth.get_user(request).username:
return redirect('/')
context = {}
context.update(csrf(request))
context['form'] = UserCreationForm()
if request.POST:
newuser_form = UserCreationForm(request.POST)
if newuser_form.is_valid():
newuser_form.save()
newuser = auth.authenticate(username=newuser_form.cleaned_data['username'],
password=newuser_form.cleaned_data['password2'])
auth.login(request, newuser)
return redirect('/')
else:
context['form'] = newuser_form
return render(request, 'user_auth/user_auth_register.html', context)
Answer the question
In order to leave comments, you need to log in
Found the answer here: https://simpleisbetterthancomplex.com/tutorial/201...
I hope it will be useful for someone
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question