On Tuesday 25 August 2009 12:56:12 pm Sandra Django wrote:
> Hi friends, I'm trying to custom the index.html of Django. For that, I'm
> writing a custom template tag (categories.py). This module should display a
> list, but don't display anything. Why? I'll describe that I did because if
> you can you explain me what is the problem, ok?
> 1) I created "templatetags" folder at the same level of model.py, inside my
> app
> 2) "templatetags" folder has inside __init___.py (it's empty), and
> categories.py
> 3) In categories.py I wrote:
> from django.template import Library
> from myproject.app.models import Category
>
> register = Library()
>
> def nav_categorylist(request):
> categories = Category.objects.all().order_by('name')[:3]
> return {'categories': categories}
>
> register.inclusion_tag('admin/index.html')(nav_categorylist)
> 4) "index.html" is in the path: myproject/templates/admin/ index.html
> 5) In index.html I put this:
> {% load categories %}
> {% for cat in categories %}
> {{ cat.name }}
> {% endfor %}
> 6) In INSTALLED_APP I put ('myproject.app.templatetags')
>
> What is the mistake? Thank's for any help
>
Two things, for INSTALL_APPS it should only be myproject.app
Django will find the templatetags directory in your apps directory.
You don't need to load the tags in your admin/index.html file, the context is
passed to it already. Should only load it if you're using other custom
filters or tags from the same app.
Your html for admin/index.html should be:
{% for cat in categories %}
{{ cat.name }}
{% endfor %}
In the html/template file that is using the nav_categorieslist tag, it should
look like this:
{% load categories %}
{% nav_categorylist %}
I normally put my load tags at the top of the template file.
If you set the TEMPLATE_LOADERS setting to include
'django.template.loaders.app_directories.load_template_source',
You can put the template tags html file in your app directory also, i.e.
app/templates/admin/index.html; I do it this way to seperate the html from
template tags from the rest of the html.
Mike
--
I feel ... JUGULAR ...
signature.asc
Description: This is a digitally signed message part.

