如何将数据呈现到 Django 中的 {% includes a.html %} 模板 [英] How to render data to a {% included a.html %} template in Django

查看:22
本文介绍了如何将数据呈现到 Django 中的 {% includes a.html %} 模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rank.html,它是许多其他模板的公开共享模板,通过 {% include rank.html %} 方法.

此模板将根据点击次数显示 48 小时热门新闻.

这是view.py:

def rank(self, request):hot_news_48h = h_mostViewed(48, 新闻, '-pv')返回渲染(请求,rank.html",{'hot_news_48h': hot_news_48h,})

h_mostViewed(48, News, '-pv') 是一个函数,可以在 48 小时内获取最多查看(点击)的帖子.它有效.

这是 rank.html:

    {% for hot_view in hot_news_48h %}<li><a href="{% url 'news:news_detail' hot_view.pk %}" ><img src="{{ MEDIA_URL }}{{ hot_view.image }}" ></a><a href="{% url 'news:news_detail' hot_view.pk %}"><h6>{{ hot_view.title }}</h6></a>

{% 结束为 %}

这里是url.py:

path('hot_news', views.rank, name="hot_news")

问题是,我只能获取html,但无法接收数据.

但是如果我放弃 {% include rank.html %} 方法并直接在每个需要此功能的模板中插入 rank.html 的代码,我可以获得数据.以 new_detail.html 模板为例:

这是view.py:

def newsDetailView(request, news_pk):news = get_object_or_404(News, id=news_pk)all_comments = NewsComments.objects.filter(news=news)news.comment_nums = all_comments.count()新闻.save()News.objects.filter(id=news_pk).update(pv=F('pv') + 1)hot_news_48h = h_mostViewed(48, 新闻, '-pv')relative_news = News.objects.filter(tag__id__in=news.tag.all()).exclude(id=news_pk)[:6]返回渲染(请求,news_detail.html",{'新闻':新闻,'all_comments': all_comments,'hot_news_48h': hot_news_48h,'relative_news':relative_news})

这里是 urls.py:

path('-', views.newsDetailView, name="news_detail"),

所以上面,我直接将rank.html的代码插入到new_detail.html中,并且可以获取数据.

我的问题是我应该做什么或更正,以便我可以在 {% include rank.html %} 方法中获取数据.因为 {% include rank.html %} 简单灵活.我不想在几个相同的模板中重复相同的代码.

非常感谢您的耐心等待!

解决方案

这个怎么样:- 在您的应用程序中创建一个文件夹templatetags"并添加一个文件news_tags.py"或将其命名为您想要的.然后你可以定义你需要的标签:

from django.template 导入库从 your_app.models 导入 your_model注册 = 库()@register.inclusion_tag('your_app/your_template.html')def hot_news(num, order):objects = News.objects.order_by(order)[:num]结果['对象'] = 对象返回结果

然后在您的模板中执行以下操作:

{% 加载 news_tags %}{% 热点新闻 48 '-pv' %}

然后像之前一样创建一个模板并在包含标签中引用它.那么它应该可以正常工作.

如果你想让它适用于多个模型,你可以看看这个:https://docs.djangoproject.com/el/2.1/ref/applications/应用程序框架允许您从字符串输入中获取模型.

I have a rank.html which is a publicly sharing template for many other templates through {% include rank.html %} method.

This template will display the 48 hours hot news base on the click number.

Here is the view.py:

def rank(self, request):
    hot_news_48h = h_mostViewed(48, News, '-pv')

   return render(request, "rank.html", {
        'hot_news_48h': hot_news_48h,})

h_mostViewed(48, News, '-pv') is a function,that can fetch most viewed(clicked) post within 48 hours.It works.

Here is the rank.html:

<ul>
    {% for hot_view in hot_news_48h %}
 <li>
    <a href="{% url 'news:news_detail' hot_view.pk %}" >
      <img src="{{ MEDIA_URL }}{{ hot_view.image }}" >
    </a>

    <a href="{% url 'news:news_detail' hot_view.pk %}">
      <h6>{{ hot_view.title }}</h6>
     </a>
</div>
</li>
  {% endfor %}
</ul>

Here is the url.py:

path('hot_news', views.rank, name="hot_news")

The problem is,I can only get the html ,but can't receive the data.

But if I give up {% include rank.html %} method and insert the rank.html's code directly inside each template which need this function, I can get the data. Take new_detail.html template as an example:

Here is the view.py:

def newsDetailView(request, news_pk):
    news = get_object_or_404(News, id=news_pk)
    all_comments = NewsComments.objects.filter(news=news)
    news.comment_nums = all_comments.count()
    news.save()
    News.objects.filter(id=news_pk).update(pv=F('pv') + 1)

    hot_news_48h = h_mostViewed(48, News, '-pv')

    relative_news = News.objects.filter(tag__id__in=news.tag.all()).exclude(id=news_pk)[:6]

    return render(request, "news_detail.html", {
        'news': news,
        'all_comments': all_comments,
        'hot_news_48h': hot_news_48h,

        'relative_news': relative_news
    })

Here is the urls.py:

path('-<int:news_pk>', views.newsDetailView, name="news_detail"),

So above,I directly inserted rank.html's code into new_detail.html and it works I can get the data.

My question is what should I do or correct,so that I can get the data in {% include rank.html %} method. Because {% include rank.html %} is simple and flexible.I don't want to repeat the same code in several same template.

Thank you so much for your patience!

解决方案

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.

这篇关于如何将数据呈现到 Django 中的 {% includes a.html %} 模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆