[Answer]-Django 1.7 'AnonymousUser' object has no attribute 'backend'

1👍

Your user creation method is wrong because I assume you are saving user in a plain text.

Two ways…

Method using set_password

Method using create_user

0👍

My working version:

def register(request):
if request.method == 'POST':
    user_form = UserForm(request.POST)
    if user_form.is_valid():
        user = user_form.save()
        user.set_password(request.POST['password'])
        user.save()
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username,password=password)
        user.backend = 'django.contrib.auth.backends.ModelBackend'
        login(request, user)
    messages.info(request, "Thanks for register!")
    return HttpResponseRedirect("/")

Leave a comment