Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django create user and log them in

Tags:

django

In a view I'm trying to create a new user and then log them in but result in a new url on success.

def create(request):

    if request.method == "POST":

        # do user creation #
        user.save()

        auth_user = authenticate(username=user.username,password=user.password)

        if auth_user is not None:
            login(request, auth_user)

            return HttpResponseRedirect('/user/account/')

    return render_to_response('create_form.html')

So, how do I maintain the user object using the HttpResponseRedirect or validate the logged in user in an unassociated view?

like image 481
Scott Avatar asked Apr 18 '10 05:04

Scott


People also ask

How do I login as user in Django?

from django.contrib.auth import authenticate, login def my_view(request): username = request.POST['username'] password = request.POST['password'] user = authenticate(request, username=username, password=password) if user is not None: login(request, user) # Redirect to a success page. ... else: # Return an 'invalid ...

How do I create a Django username and password?

The correct way to create a user in Django is to use the create_user function. This will handle the hashing of the password, etc..


1 Answers

The session middleware should handle this transparently. If you're finding that this isn't the case then you should be looking in that direction for problems.

like image 51
Ignacio Vazquez-Abrams Avatar answered Sep 22 '22 15:09

Ignacio Vazquez-Abrams