I'm new in Django. I tried to create an auto-login after registration users. But I got fail. I created a custom auth backend for username and password asuthentification.
class EmailBackend(object):
def authenticate(self, username=None, password=None):
user_cls = get_user_model()
try:
user = user_cls.objects.get(email=username)
if user.check_password(password):
return user
except user_cls.DoesNotExist:
return None
def get_user(self, user_id):
user_cls = get_user_model()
try:
return user_cls.objects.get(pk=user_id)
except user_cls.DoesNotExist:
return None
AUTHENTICATION_BACKENDS = (
'user.back_auth.EmailBackend',
'django.contrib.auth.backends.ModelBackend',
)
I try to make autologin after registration for users:
def register(request):
if request.method == 'POST':
user_form = UserForm(request.POST)
if user_form.is_valid():
user = user_form.save()
messages.info(request, "Thanks for registering. You are now logged in.")
user = authenticate(username=request.POST['username'],
password=request.POST['password'])
login(request, user)
return HttpResponseRedirect("/")
else:
user_form = UserForm()
return render(request,
'user/register.html',
{'user_form': user_form } )
But, I'm getting the next:
AttributeError at /user/register/ 'AnonymousUser' object has no attribute 'backend'
save
method in UserForm or Can you make sure user is created or what ? And also tell how password looks like ( plaintext or hash ) ? – Raja Simon