2
\$\begingroup\$

Can you say, is this a bad code for registration in Django? Do I need to fix something or use a pip instead? (I am learning)

def register(request):
 error_messages = {}
 if request.POST:
 form = UserCreationForm(request.POST)
 if form.is_valid():
 new_user = form.save()
 user = authenticate(username=request.POST["username"], password=request.POST["password1"])
 login(request, user)
 return redirect('index')
 else:
 form = UserCreationForm()
 return redirect('register')
 return render(request, 'account/register.html',
 {'title': 'Registration', 'error_messages': error_messages})
SuperBiasedMan
13.5k5 gold badges37 silver badges62 bronze badges
asked Jan 31, 2016 at 19:23
\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

From what you can find on the docs you are pretty close. I may change a couple things a little just to be on the safe side:

Change:

user = authenticate(username=request.POST["username"], password=request.POST["password1"])

To:

user = authenticate(username=form.cleaned_data['username'], form.cleaned_data['password1'])

The first way is not wrong, but using validated data on forms is a better practice than using the raw post request. Is better to be consistent.

answered Feb 12, 2016 at 20:43
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.