tags:

views:

94

answers:

1

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?

+4  A: 

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.

Ignacio Vazquez-Abrams
So, once I redirect to the new page ('/user/account/'), how do I get the 'user' object? Does HttpResponseRedirect include a 'request' object? I didn't think it did...
Scott Willman
You get it from `request` since the auth middleware pulls it from the session and puts it there. And all `HttpResponseRedirect` does is redirect the browser somewhere else. The new request generates a new `request`.
Ignacio Vazquez-Abrams
yeah, you're right. Once I get to the new view, the request object has request.user, but I can't figure out why it's not passing it to the templates of that view??
Scott Willman
That would be because you need to handle the `RequestContext`. http://docs.djangoproject.com/en/dev/ref/templates/api/#id1
Ignacio Vazquez-Abrams
That was it! Huge thanks for the help! `return render_to_response('people/account.html',context_instance=RequestContext(request))`
Scott Willman