Django Templates Variable Resolution
Hey there, it's been a week with Django back here so don't mind me if the question is stupid, though I searched over stackoverflow and google with no luck.
I've got a simple model called Term (trying to implement tags and categories for my news module) and I have a template tag called taxonomy_list which should output all the terms assigned to a post in a comma-separated list of links. Now, my Term model does not have a permalink field, but it gets there from within my view and is being passed on to the template. The permalink value looks fine inside the template, but doesn't load up into my template tag.
To illustrate this I got bits from my code. Here's my custom template tag called taxonomy_list:
from django import template
from juice.taxonomy.models import Term
from juice.news.models import Post
register = template.Library()
@register.tag
def taxonomy_list(parser, token):
tag_name, terms = token.split_contents()
return TaxonomyNode(terms)
class TaxonomyNode(template.Node):
def __init__(self, terms):
self.terms = template.Variable(terms)
def render(self, context):
terms = self.terms.resolve(context)
links = []
for term in terms.all():
links.append('<a href="%s">%s</a>' % (term.permalink, term.name))
return ", ".join(links)
Here's my single post view:
# single post view
def single(request, post_slug):
p = Post.objects.get(slug=post_slug)
p.tags = p.terms.filter(taxonomy='tag')
p.categories = p.terms.filter(taxonomy='category')
# set the permalinks
for c in p.categories:
c.permalink = make_permalink(c)
for t in p.tags:
t.permalink = make_permalink(t)
return render_to_response('news-single.html', {'post': p})
And this is what I do inside my template to illustrate two methods of accessing the categories:
Method1: {% taxonomy_list post.categories %}
Method2: {% for c in post.categories %}{{c.slug}} ({{c.permalink}}),{% endfor %}
What's interesting is that Method number 2 works fine, but Method number 1 says that my .permalink field is undefined, this probab开发者_C百科ly means that the variable resolution is not done as I'm expecting, since the "extra" permalink field is left out.
I thought that maybe the variable is not recognizing the field as it is not defined in the model so I tried assigning it a "temporary" value inside the model, but that didn't help either. Method1 contained "temporary" in links, while method2 was working out correctly.
Any ideas?
Thanks!
It's not really a question of variable resolution. The issue is how you're getting the terms from the Post object.
When, inside your template tag, you do for term in terms.all()
, the all
is telling Django to re-evaluate the queryset, which means querying the database again. So, your carefully-annotated terms are getting refreshed with new objects from the database, and the permalink
attribute is getting overridden.
It will probably work if you just drop the all
- so you just have for term in terms:
. This will re-use the existing objects.
精彩评论