Saturday, November 25, 2017

Re: How can I learn to make a user login?

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?

On Saturday, November 25, 2017 at 3:14:25 PM UTC-5, Daniel Roseman wrote:
On Saturday, 25 November 2017 19:59:10 UTC, Tom Tanner wrote:
To put it another way: Is there something like `UserCreationForm`, but for logging in users?

On Saturday, November 25, 2017 at 2:20:11 PM UTC-5, Tom Tanner wrote:
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?

Did you read the docs, where these are fully documented?
--
DR. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac8ae345-b886-40e6-8c9f-fd1b6d75c54e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment