简体   繁体   中英

How to render different data to the same page by two different views - Django

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
    })

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 .

I have tried :

 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
     })

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

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

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

news:news_detail is the url of view def newsDetailView

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

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

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.

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
    })

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM