How to render data to a {% included a.html %} template in Django

徘徊边缘 提交于 2019-11-29 18:07:34

How about this: - Create a folder "templatetags" in your application and add a file "news_tags.py" or name it what you want. Then you can define the tags you need:

from django.template import Library
from your_app.models import your_model

register = Library()

@register.inclusion_tag('your_app/your_template.html')
def hot_news(num, order):
    objects = News.objects.order_by(order)[:num]

    result['objects'] = objects

    return result

In your templates you then do the following:

{% load news_tags %}
{% hot_news 48 '-pv' %}

Then create a template as your already did and reference it in the inclusion tag. Then it should work properly.

If you want it to work for multiple models you can have a look at this: https://docs.djangoproject.com/el/2.1/ref/applications/ The apps framework allows you to fetch models from a string input.

I finally solved the issue by Creating custom context processor.https://www.youtube.com/watch?v=QTgkGBjjVYM

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!