Question

Ok, after fiddling around a bit and installing debug_toolbar, the problem disappears after removing the following line,

urlpatterns = patterns('',
    # some other patterns
    url(r'^(?P<tag_name>.+)/$','myapp.views.tag_details',name = 'tag_details'),#<<---
    #!!! removing above line solves the problem
     ) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)


urlpatterns += patterns(
'django.contrib.auth.views',

url(r'^login/', 'login',
    {'template_name' : 'login.html'},
    name = 'constituency_login'),

url(r'^logout/', 'logout',
    {'next_page' : 'constituency_login'},
    name = 'constituency_logout'),

)

Why does it break my login and logout patterns?


I had implemented login and logout a while back, now suddenly while I was trying to implement another feature, the login and logout just doesn't show somehow.

I can login via the admin and the site can show that I am logged in but the 'login' url shows an empty page and 'logout' does nothing.

Here's how I implemented it:

urls.py in project folder

urlpatterns += patterns(
'django.contrib.auth.views',

url(r'^login/', 'login',
    {'template_name' : 'login.html'},
    name = 'myapp_login'),

url(r'^logout/', 'logout',
    {'next_page' : 'myapp_login'},
    name = 'myapp_logout'),
)

template/login.html

{% extends "base.html" %}
{% load staticfiles %}

{% block content%}

{% if form.errors %}
<p class="alert"> Your username and/or password didn't match </p>
{% endif%}

<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
{% csrf_token %}

        <div class="input-group ">
            <span class="input-group-addon" ><span class="glyphicon glyphicon-user"></span></span>
            <input type="text" class="form-control" placeholder="Username" id="{{ form.username.id_for_label }}" maxlength="30" name="{{ form.username.html_name }}" />

        </div>
        <div class="input-group ">
            <span class="input-group-addon" ><span class="glyphicon glyphicon-lock"></span></span>
            <input type="password" class="form-control" placeholder="Password" id="{{ form.password.id_for_label }}" name="{{ form.password.html_name }}" type="text" />
        </div>


<input class="btn" type="submit" value="login" />
<input type="hidden" name="next" value="{{ next }}" />
</form>
</div>
</div>
</div>
{% endblock %}

settings.py

LOGIN_URL = 'myapp_login'
LOGOUT_URL = 'myapp_logout'
LOGIN_REDIRECT_URL = 'index'

I haven't made changes to any of these. Infact, I've added multiple features since then.

Was it helpful?

Solution

Django always returns the first regex match in urlpatterns. Your regex for your tag_details view matches all non-empty patterns with .+. As your login and logout urls come after the tag_details view in your url patterns, both views get matched to your tag_details view.

You either have to make sure that your tag_details view comes last in your patterns, or use some prefix, e.g. /tag/<tag>/. It's also good practice to make your regex patterns as restrictive as possible. If you're not allowing special characters in your tag name, i.e. slashes etc., it would be better to use a pattern such as r'^(?P<tag_name>[\w]+)/$'.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top