如何通过两个不同的视图将不同的数据呈现到同一页面-Django [英] How to render different data to the same page by two different views - Django

查看:301
本文介绍了如何通过两个不同的视图将不同的数据呈现到同一页面-Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在建立一个新闻网站。我需要显示48小时内观看次数最多的新闻,这部分在detail.html页面中。现在,我正在使用这种方法。

I'm building a news website.I need display 48 hours most viewed news, this part is in the detail.html page. Now I'm using this method.

def newsDetailView(request, news_pk):
    news = get_object_or_404(News, id=news_pk)
    News.objects.filter(id=news_pk).update(pv=F('pv') + 1)
    time_period = datetime.now() - timedelta(hours=48)
    host_news=news.objects.filter(date_created__gte=time_period).order_by('-pv')[:7]

    return render(request, "news_detail.html", {
        'news': news,
        'host_news' : host_news
    })

效果很好,但我的问题是,为了方便使用缓存,我想将hot_news函数与 def newsDetailView 分开。

It works very well, but my question is ,in oder to use cache conveniently,I want to separate the hot_news functions from def newsDetailView.

我尝试过:

 def hot_news(request):
     time_period = datetime.now() - timedelta(hours=48)
     hot_news =News.objects.filter(add_time__gt=time_period).order_by('-pv')[:7]

     return render(request, "news_detail.html", {
         'most_viewedh': most_viewedh
     })

但是我无法获取数据,在 detail.html 。我猜这是因为网址。

However I can't get the data, in detail.html. I guess the issue is because the url.

<$ c $中 detail.html 的链接c> index.html 是

the link of the detail.html from the index.html is

 <a href="{% url 'news:news_detail' news.pk %}">

news:news_detail 是视图的网址 def newsDetailView

因此,该网址直接指向 def newsDetailView ,与 def hot_news无关

So the url is directly to def newsDetailView and has nothing two do with def hot_news.

该怎么办,以便可以将数据从 def hot_news 呈现到与 def newsDetailView 是吗?

What should I do, so that I can render the data from def hot_news to the same page as def newsDetailView does?

推荐答案

所以您正确地说是因为您要访问的网址是'news:news_detail'是唯一加载的视图。如果您确实想从其他视图加载数据,则可以使用ajax来加载hot_news数据并将其插入页面。

So you are correct in saying that beacuse the url you are going to is 'news:news_detail' that is the only view that is loaded. If you did want to load data from another view you could use ajax to load just the hot_news data and insert that into the page.

尽管您想要实现所有这些功能不需要缓存hot_news。您可以改为使用django的低级缓存api,如下所示:

Although if all that you want to achieve is caching of hot_news this is not required. You could instead make use of django's low level cache api like this:

from django.core.cache import cache

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


    # Get host_news from the cache
    host_news = cache.get('host_news')

    # If it was not in the cache, calculate it and set cache value
    if not host_news:

        time_period = datetime.now() - timedelta(hours=48)
        host_news=news.objects.filter(date_created__gte=time_period).order_by('pv')[:7]        

        # Sets the cache key host_news with a timeout of 3600 seconds (1 hour)
        cache.set('host_news', host_news, 3600)

    return render(request, "news_detail.html", {
        'news': news,
        'host_news' : host_news
    })

低位文档evel缓存api在这里: https:/ /docs.djangoproject.com/en/2.0/topics/cache/#the-low-level-cache-api

Docs on the low level cache api are here: https://docs.djangoproject.com/en/2.0/topics/cache/#the-low-level-cache-api

您可能还需要查看如果尚未设置,请在settings.py中设置CACHES

You may also have to look at setting CACHES in your settings.py if you haven't already done so

这篇关于如何通过两个不同的视图将不同的数据呈现到同一页面-Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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