views:

508

answers:

3

Hi guys, i would like to make a django custom tag for display 10 entry title from the category where the user is reading a article... but how ill do?, i need to send the category from the actual entry...

Any idea?

Thanks

+1  A: 

Why a custom tag? It's probably better, cleaner, to add a method to the article model, and call that from the template. Depending on your model, the method would be practically trivial.

Chipaca
yep. something like Article.get_related_titles(10)
Evgeny
hi guys, thanks for reply , im new with Django, sorry :).. im using Django Generic View Date Based, im lost with this part... making a method in the model??, i know what is a method but, you are talking about queryset in the model?
Asinox
In the model class definition you can add arbitrary methods (in the same manner as you'd add __unicode__() or get_absolute_url()) whose return values are automatically made available in your templates.
cpharmston
ok, i make a method get_category(self) return self.category, but my category field is a ManyToManyField, and is returning <django.db.models.fields.related.ManyRelatedManager object at 0x9c9bc2c> just i want to show 10 articles in right side of the article details template.... im lost
Asinox
-1. Doing this in the model is a silly idea. It's not good to have instance methods that return other instances, and a classmethod wouldn't have access to the current article to get the related ones. A template tag is definitely the way to go.
Daniel Roseman
Plus, you can't pass arguments from a template to a method, so you'd need a template tag for that anyway.
Daniel Roseman
+1  A: 

Check here for making custom tags:

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags

You would need to pass the relevant category object to the template context.

Gabriel Ross
+3  A: 

The best way to do this would be with an inclusion tag. This is a tag that renders a template fragment which renders the 10 related articles.

You just pass in the current article into the tag, and return the context for the template fragment - ie the related articles.

@register.inclusion_tag('related_articles.html')
def related_articles(article, count):
    category = article.category
    articles = category.article_set.exclude(id=article.id)[:count]
    return {'articles': articles}

You'll need a related_articles.html file in your templates directory which outputs the articles. Then, to call it from your main template, you would just do

{% related_articles article 10 %}

where article is the name of the article object.

Daniel Roseman
http://www.b-list.org/weblog/2006/jun/07/django-tips-write-better-template-tags/ James Bannet discribes a technique, how to make even more generic tag. i.e. you can use one template_tag for the Models "News", "Article", "Event"... It doesnt fint excacly to want you describe, but worth reading.
vikingosegundo
@Daniel: my browser tells me, that I down-voted your answer. I didnt intend to, and i surely didnt click it. I dont know, how it happened. It says that you need to edit the answer, so that i can delete my down-vote. Sorry for the inconvenience...
vikingosegundo
No problem, I edited it. +1 for the link to James's piece, though.
Daniel Roseman
Thanks Daniel Roseman, "is working", i know is working, just than know i have a "comun error" about relationshipCaught an exception while rendering: 'ManyRelatedManager' object has no attribute 'noticia_set'my field category is type ManyToManyField
Asinox
In that case you'll need to pass the category from the template as well as the article, as there's no way to get from the article to the particular category you need.
Daniel Roseman