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?
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 ...
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..
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With