I'm using a (compatible) Custom User model. The registering part of django-registration is working perfectly. However, when I take the user to the log-in page from a navbar, The user is never really authenticated. If I put in a wrong password it will correctly through and error, but when username and password are correct, the user is simply redirected to the correct page, just without getting authenticated.
My register/urls.py
:
urlpatterns = [
# a custom RegistrationForm that works perfectly
url(r'^register/$',
RegistrationView.as_view(form_class=GeneralUserForm),
name='registration_register'),
url(r'^', include('registration.backends.hmac.urls')),
]
My register/templates/registration/login.html
:
{% extends "base.html" %}
{% load i18n %}
{% block content %}
<form method="post" action=".">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="{% trans 'Log in' %}" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
<p>{% trans "Forgot password" %}? <a href="{% url 'auth_password_reset' %}">{% trans "Reset it" %}</a>!</p>
<p>{% trans "Not member" %}? <a href="{% url 'registration_register' %}">{% trans "Register" %}</a>!</p>
{% endblock %}
Relevant snippets from settings.py
:
# Setting custom User Model
AUTH_USER_MODEL = 'register.GeneralUser'
# for guardian to work
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend', # this is default
'guardian.backends.ObjectPermissionBackend',
)
# Setting limit on days
ACCOUNT_ACTIVATION_DAYS = 7
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Occam's Razor, always.
My problem was much simpler:
{% if user.authenticated %} # should simply be user.is_authenticated
<li>Welcome {{ user.username }}</li>
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Logout</a></li>
{% else %}
<li class="active"><a href="#">Home</a></li>
<li><a href="/accounts/register">Register</a></li>
<li><a href="/accounts/login">Login</a></li>
<li>Welcome {{ user.date_joined }}</li>
{% endif %}