R
R
R.2017-08-15 18:39:59
Django
R., 2017-08-15 18:39:59

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

2 answer(s)
R
R., 2017-08-16
@ron6500

Found the answer here: https://simpleisbetterthancomplex.com/tutorial/201...
I hope it will be useful for someone

R
rumsey, 2017-08-15
@rumsey

See how it's done in django - UserCreationForm

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question