How can I learn to make a user login?

30 views
Skip to first unread message

Tom Tanner

unread,
Nov 25, 2017, 1:57:04 PM11/25/17
to Django users
I've read this tutorial on making a simple user registration form. But what about a form for just logging in existing users? I can make a registration form with `views.py` looking like this:

 registration_form.save()
 email
= registration_form.cleaned_data.get("email")
 raw_password
= registration_form.cleaned_data.get("password1")
 user
= authenticate(username=email, password=raw_password)
 login
(request, user)
 
return redirect(home_slug())

Is there an example of how to make the backend for logging in users?

Daniel Roseman

unread,
Nov 25, 2017, 2:03:36 PM11/25/17
to Django users
Why do you think you need a "backend"? You've got everything you need here already. 
--
DR.

Tom Tanner

unread,
Nov 25, 2017, 2:20:11 PM11/25/17
to Django users
I guess what I mean is...

What’s function do I need to use that:
A) logs in the user
B) returns an error if there’s a problem logging in?

Tom Tanner

unread,
Nov 25, 2017, 2:59:10 PM11/25/17
to Django users
To put it another way: Is there something like `UserCreationForm`, but for logging in users?

Daniel Roseman

unread,
Nov 25, 2017, 3:14:25 PM11/25/17
to Django users
Did you read the docs, where these are fully documented?
--
DR. 

Tom Tanner

unread,
Nov 25, 2017, 3:57:55 PM11/25/17
to Django users
I see Django has an auth URLfor logging in. How do I construct the login form?

I have a template that will have both login and register forms on it. My view `login_register` looks something like this:

def login_register(request, template="pages/login_register.html"):
 
'''
 Display registration and login forms.
 Process user registration.
 Process user login.
 '''

 
if request.method=="POST":
  registration_form
= UserCreationForm(request.POST)
 
if registration_form.is_valid():
   registration_form
.save()
   username
= registration_form.cleaned_data.get("username")
   raw_password
= registration_form.cleaned_data.get("password1")
   user
= authenticate(username=username, password=raw_password)
   login
(request, user)
   
return redirect(home_slug())
 
else:
   registration_form
= UserCreationForm()
   
return render(request, template, {"registration_form": registration_form})

The template for this view:
 <div">
 
<h2>Log in</h2>
 
<form method="post">
   {% csrf_token %}
   {{ login_form.as_p }}
 
</form>
 
</div>
 
<div>
 
<h2>Register</h2>
 
<form method="post">
   {% csrf_token %}
   {{ registration_form.as_p }}
   
<button type="submit">Register</button>
 
</form>
 
</div>

It seems like `UserCreationForm` makes the register form. So how do I make the login form?

Daniel Roseman

unread,
Nov 25, 2017, 4:11:11 PM11/25/17
to Django users
I literally just pointed you to the page in the docs that lists the login form and view.
--
DR.
Reply all
Reply to author
Forward
0 new messages