Django urls.py not rendering correct template

1.2k views Asked by At

This is my urls.py

"""s7h URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/1.8/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  url(r'^$', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  url(r'^$', Home.as_view(), name='home')
Including another URLconf
    1. Add an import:  from blog import urls as blog_urls
    2. Add a URL to urlpatterns:  url(r'^blog/', include(blog_urls))
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    url(r'^articles/', include('article.urls')),
    url(r'^',include('django.contrib.auth.urls')),
    url(r'^projects/', include('project.urls')),

    #Serve Media Files
    url(r'^media/(?P<path>.*)$', 'django.views.static.serve', {
            'document_root': settings.MEDIA_ROOT,
        }),

    #subscribe
    url(r'^subscribe/$', 'article.views.subscribe'),
    url(r'^verify/$', 'article.views.activateSubscriber'),

    # user auth urls
    url(r'^home/$', 's7h.views.home'),
    url(r'^', 's7h.views.home'),
    url(r'^accounts/login/$', 's7h.views.login'),
    url(r'^accounts/auth/$', 's7h.views.auth_view'),
    url(r'^accounts/logout/$', 's7h.views.logout'),
    url(r'^accounts/loggedin/$', 's7h.views.loggedin'),
    url(r'^accounts/invalid/$', 's7h.views.invalid_login'),
    # user registration urls
    url(r'^accounts/register/$', 's7h.views.register'),
    url(r'^accounts/register_success/$', 's7h.views.register_success'),
    url(r'^accounts/register_auth/$', 's7h.views.register_auth'),

    url(r'^contact/$', 's7h.views.contact'),


]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

handler400 = 's7h.views.custom_400'
handler403 = 's7h.views.custom_403'
handler404 = 's7h.views.custom_404'
handler500 = 's7h.views.custom_500'

if not settings.DEBUG:
    from django.contrib.staticfiles.urls import staticfiles_urlpatterns

    urlpatterns += staticfiles_urlpatterns()

This is my views.py

from django.shortcuts import render, render_to_response, RequestContext
from django.http import HttpResponseRedirect
from django.contrib import auth,messages
from django.core.context_processors import csrf
from django.template import RequestContext
from django.template.loader import get_template
from forms import MyRegistrationForm
from article.models import Article, ArticleCategory
from project.models import Project, ProjectCategory

def home(request):
    args={}
    article = Article.objects.earliest("-pub_date")
    project = Project.objects.earliest("-pub_date")
    article_category = ArticleCategory.objects.all()
    project_category = ProjectCategory.objects.all()
    args.update(csrf(request))
    args['article'] = article
    args['project'] = project
    args['project_categories'] = project_category
    args['article_categories'] = article_category
    args['loggedin'] = request.session.get('email', None)
    return render_to_response('home.html', args)

def login(request):
    c = {}
    c.update(csrf(request))
    return render_to_response('login.html', c)

def auth_view(request):
    username = request.POST.get('username', '')
    password = request.POST.get('password', '')
    user = auth.authenticate(username=username, password=password)

    if user is not None:
        auth.login(request, user)
        request.session['email'] = user.email
        return render(request, "loggedin.html", locals(),context_instance=RequestContext(request))
    else:
        return HttpResponseRedirect('/accounts/invalid/')

def loggedin(request):
    return render_to_response('loggedin.html', {})

def invalid_login(request):
    return render_to_response('invalid_login.html')

def logout(request):
    auth.logout(request)
    return HttpResponseRedirect('/accounts/login/')

This is my template for login

{% extends "user_forms.html" %}

{% block title %}
    S7H - Login
{% endblock %}

{% block main %}
<span class="fa fa-user bigicon"></span>
<h2>Enter Details</h2>
<form action = "/accounts/auth/" method = "POST">{% csrf_token %}

    <p><input type="text" name="username" required placeholder="Username" autofocus></p>
    <p><input type="password" name="password" required placeholder="Password"></p>

<button class="btn btn-default" type="submit" name="submit" />Sign In</button>
<a href="/accounts/register/" class="btn btn-default">Sign Up</a>
</form>
<small><a href="/password_reset/">Forgot your password?</a></small><br />
{% if messages %}
{% for message in messages %}
<small {% if message.tags %} class="{{message.tags}}" {% endif %}>{{message}}</small>
{% endfor %}
{% endif %}

{% endblock %}

Now when I do http://127.0.0.1:8000/accounts/login/ , Django should render login.html because url accounts/login/ leads me to def login(request): and there in render_to_response() I have mentioned login.html to get rendered but instead of it, Django renders my home.html file. URL of the browser changes to http://127.0.0.1:8000/accounts/login/ when I press the login button on my home page but it keeps on rendering the same home.html template.

This thing is happening with other urls also. Every url is rendering home.html template.

I double checked all my urls, views and templates, I have not been able to figure out my mistake. Django is also not giving any Exceptions and Error messages.

Also in the terminal the HTTP response is 200 OK :/

[08/Jun/2015 01:38:40]"GET /accounts/login/ HTTP/1.1" 200 10201

Help me!

1

There are 1 answers

1
Daniel Roseman On BEST ANSWER

Your s7h.views.home pattern has no terminating $ so it matches every URL.