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

问题描述

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

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

此模板将基于点击次数显示48小时热门新闻。

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

这里是view.py:

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')是一个函数,可以在48小时内获取大多数观看(点击)的帖子。

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

这是rank.html:

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>

这是url.py:

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

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

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

但是如果我放弃{%include将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()
    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
    })

这是urls.py:

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

S在上面,我直接将rank.html的代码插入到new_detail.html中,它可以正常工作,我可以获取数据。

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

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

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.

非常感谢您的耐心!

推荐答案

此:
-在应用程序中创建一个文件夹 templatetags,并添加文件 news_tags.py或将其命名为您想要的名称。然后可以定义所需的标签:

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中将数据呈现到{%included a.html%}模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 02:19