Search code examples
pythondjangodjango-templatesnavigation

django Navigation, need help on where to put the tags.py file


I was trying to do what Ben Liyanage answer to handing Navigation in Django using tags. Here is the link I was reading Navigation in django.

I was trying to use this in my project to learn how to do navigation in Django. When I opened my login.html for example I got the following error: 'tags' is not a valid tag library: Template library tags not found, tried django.templatetags.tags,django.contrib.admin.templatetags.tags,django.contrib.staticfiles.templatetags.tags

I am actually not sure where the tags.py file is supposed to go. Here is my file path and code:

## file path
-- Main project folder
  -- manage.py
  -- .. other files ..
  -- src
     -- urls.py
     -- .. other files ..
  -- templates
     -- base.html
  -- app folder
    -- templates
       -- app name
          -- index.html
          -- login.html
          -- register.html
    -- tags.py
    -- .. other files ..

## urls.py
from django.conf.urls import patterns, url

from game import views

urlpatterns = patterns('',
    url(r'^$', views.index, name='index'),
    url(r'^upload/$', views.upload_file, name='upload'),
    url(r'^successful_upload/$', views.successful_upload, name='successful_upload'),
    url(r'^play/$', views.play, name='play'),
    url(r'^registration/$', views.register, name='register'),
    url(r'^successful_registeration/$', views.successful_registeration, name='successful_registeration'),
    url(r'^login/$', views.login, name='login'),
    url(r'^logout/$', views.logout, name='logout')
)

## tags.py
from django import template

register = template.Library()

@register.tag
def active(parser, token):
    import re
    args = token.split_contents()
    template_tag = args[0]
    if len(args) < 2:
        raise template.TemplateSyntaxError, "%r tag requires at least one argument" % template_tag
    return NavSelectedNode(args[1:])

class NavSelectedNode(template.Node):
    def __init__(self, patterns):
        self.patterns = patterns
    def render(self, context):
        path = context['request'].path
        for p in self.patterns:
            pValue = template.Variable(p).resolve(context)
            if path == pValue:
                return "-active"
        return ""

## base.html
{% load tags %}
{% url 'index' as home %}
{% url 'upload' as upload %}
{% url 'play' as play %}
{% url 'register' as contact %}
{% url 'login' as login %}

<div id="navigation">
    <a class="{% active request home %}" href="{{ home }}">Home</a>
    <a class="{% active request upload %}" href="{{ upload }}">Upload</a>
    <a class="{% active request play %}" href="{{ play }}">Play</a>
    <a class="{% active request contact %}" href="{{ contact }}">Contact</a>
    <a class="{% active request login %}" href="{{ login }}">Login</a>
</div>

## login.html
{% extends "base.html" %}
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
<form action="/game/login/" method="post">
{% csrf_token %}
<table border='0'>
<div class="fieldWrapper"><tr><td>
        {{ form.user_name.errors }}</td><td></td></tr><tr><td>
        <label for="id_user_name">User Name:</label></td><td>
        {{ form.user_name }}</td></tr>
    </div>
    <div class="fieldWrapper"><tr><td>
        {{ form.password.errors }}</td><td></td><tr><td>
        <label for="id_password">Password:</label></td><td>
        {{ form.password }}</td></tr>
    </div>
</table>
<input type="submit" value="Login" />
</form>

Solution

  • You should create a templatetags package under your app folder and put the tags.py in it:

    project_folder/
        app_folder/
            __init__.py
            models.py
            templatetags/
                __init__.py
                tags.py
            views.py
    

    Do not forget to include an empty __init__.py file in your templatetags directory otherwise Python interpreter will think that it is an ordinary directory, not a Python package. See https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/ for details.