views:

307

answers:

4

Hello world!

Im trieng to build my own template tags Im have no idea why I get the errors I get, im following the django doc's.

this is my file structure of my app:

pollquiz/
    __init__.py
    show_pollquiz.html
    showpollquiz.py

This is showpollquiz.py:

from django import template
from pollquiz.models import PollQuiz, Choice
register = template.Library()

@register.inclusion_tag('show_pollquiz.html')
def show_poll():
    poll = Choice.objects.all()
    return { 'poll' : poll }

html file:

<ul>
{% for poll in poll 
    <li>{{ poll.pollquiz }}</li>
{% endfor 
</ul>

in my base.html file im am including like this

{% load showpollquiz %}
and
{% poll_quiz %}

Bu then I get the the error:

Exception Value: Caught an exception while rendering: show_pollquiz.html

I have no idea why this happens. Any ideas? Please keep in mind Im still new to Django

+2  A: 

You forgot to close your template tags... Also, you should change the name in the for tag, you can't have for poll in poll:

<ul>
{% for p in poll %} <!--here-->
    <li>{{ p.pollquiz }}</li>
{% endfor %} <!--and here-->
</ul>

Also notice you're not using the inclusion tag you defined at all. I think you got some code mixed up, try to go over a tutorial start to end and things will be clearer.

Yuval A
Hi there.I closed the endfor tag but the same error still come up
Harry
The docs you refer to are for Django 0.95. The latest ones are over here: http://docs.djangoproject.com/en/dev/ref/templates/api/
Stijn Debrouwere
fixed! thanks :)
Yuval A
A: 

I'd not bother with writing your own template tags: take things one step at a time and stick to the basics for now. There's nothing wrong with {% include 'show_pollquiz.html' %}

Stijn Debrouwere
See http://stackoverflow.com/questions/2357493/why-were-the-original-authors-of-django-against-include-tags
Dominic Rodger
+1  A: 

Shouldn't all custom filters be inside the templatetags directory?

templatetags/
    __init__.py
    showpollquiz.py

then

@register.inclusion_tag('show_pollquiz.html')

looks in MY_TEMPLATE_DIR/show_pollquiz.html for the template

dotty
yes!thats what I also figured out! Your correct
Harry
A: 

I found the problem. The problem was that the @register.inclusion_tag('show_pollquiz.html') inclusion tag is obviously looking for the template in the default_template directory. So that is why i got the error.

According to me this is not clear in the documentation. But I guess its how it is, being a template and all...

Oh , well.

Now, how would I put the @register.inclusion_tag('show_pollquiz.html') to look in the same folder as the app? under templatetags/?

Harry