I would like to make a django custom tag to display 10 entry titles from the category where the user is reading an article. How can I do this? I need to pass the category from the actual entry.
相关问题
- Django __str__ returned non-string (type NoneType)
- Django & Amazon SES SMTP. Cannot send email
- Django check user group permissions
- Django restrict pages to certain users
- UnicodeEncodeError with attach_file on EmailMessag
相关文章
- Profiling Django with PyCharm
- Why doesn't Django enforce my unique_together
- MultiValueDictKeyError in Django admin
- Django/Heroku: FATAL: too many connections for rol
- Django is sooo slow? errno 32 broken pipe? dcramer
- Django: Replacement for the default ManyToMany Wid
- Upgrading transaction.commit_manually() to Django
- UnicodeEncodeError when saving ImageField containi
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.
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
where
article
is the name of the article object.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.
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.