0π
I would try rearranging the context_processors
and put auth on top. If that doesnβt work, can you please post your custom authentication backend?
settings.py:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
# Put auth on top
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.messages.context_processors.messages',
],
},
},
]
Additionally, I would make the following changes to cleanup your views a little:
views.py:
from django.contrib import messages
from django.core.urlresolvers import reverse
def log_in(request):
form = LogIn(request.POST or None)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
user = authenticate(email=email, password=password)
# May also help with your dilemma
if user is not None:
login(request, user)
# Be sure to change this to the appropriate template name
return HttpResponseRedirect(reverse('template_name'))
else:
messages.warning(request, "Username or password is incorrect.")
return render(request, 'events/log_in.html', {'form':form})
def log_in_success(request):
# "{}".format(value) is a more reliable string substitution
return HttpResponse("{} has logged in!".format(request.user))
β β β EDIT:
I would remove your custom backend and override the functions on your form instead. Itβll save you a lot of trouble going forward.
class RegistrationForm(forms.ModelForm):
password1 = forms.CharField(widget=forms.PasswordInput(),
label="Password")
password2 = forms.CharField(widget=forms.PasswordInput(),
label="Password (again)")
class Meta:
model = Student
fields = ('firstname', 'lastname', 'email', 'password1', 'password2', 'is_second_year')
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password1")
password2 = self.cleaned_data.get("password2")
email = self.cleaned_data.get("email")
if password1 and password2:
if password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def clean_email(self):
email = self.cleaned_data["email"]
if email and Student.objects.filter(email=email):
raise forms.ValidationError("This email has already been registered")
return email
class LogIn(forms.Form):
email = forms.EmailField(label="Email")
password = forms.CharField(widget=forms.PasswordInput(), label="Password")
0π
You have to define your bakend into the settings.AUTHENTICATION_BACKENDS list:
AUTHENTICATION_BACKENDS = ['my_app.student_backend.StudentBackend']
Otherwise the get_user
method in django.contrib.auth
which contains the following lines:
user = None
...
if backend_path in settings.AUTHENTICATION_BACKENDS:
backend = load_backend(backend_path)
user = backend.get_user(user_id)
...
return user or AnonymousUser()
would return AnonymousUser()
Optionally you can set your backend when calling login
method (if there are more than one auth backends):
if form.is_valid():
email = form.cleaned_data.get("email")
password = form.cleaned_data.get("password")
user = authenticate(email=email, password=password)
login(request, user, 'my_app.student_backend.StudentBackend')
print request.user.email
return HttpResponseRedirect('/log_in_success/')
This will save the given backend for this user into the session.
- [Django]-Serializer return incorrect output
- [Django]-Django admin β prevent objects being saved, and don't show the user confirmation message