簡體   English   中英

如何在get_context_data RSS Feed django 2.2中訪問數據

[英]How to access data in get_context_data RSS Feed django 2.2

我試圖訪問受在修改“get_context_data”函數返回的語境詞典LatestVideosFeed這樣我就可以在“新聞提要”我試圖讓使用它,因為返回的上下文包含的作者視頻。 我一直在關注這些文檔https://docs.djangoproject.com/en/2.2/ref/contrib/syndication/ ,但我不知道如何訪問從get_context_data(self, item, **kwargs):它可以工作,當我在返回之前進行調試打印時,它會返回一個字典,其中最后一個條目是上載視頻的用戶。 調試打印為print(context['author']) ,每次與提要進行交互時,它都會按預期返回。

feeds.py

class LatestVideosFeed(Feed):
    link = '/video-feeds/'
    description = 'New Video Posts'

    def items(self):
        return VideoPost.objects.all()

    def get_context_data(self, item, **kwargs):
        context = super().get_context_data(**kwargs)
        context['author'] = item.author
        print(context['author'])
        return context

    def item_title(self, item):
        return item.title

    def item_description(self, item):
        return item.description

    def item_link(self, item):
        return reverse('video_post', args=[item.pk])

views.py

def video_list(request):
    feeds = feedparser.parse('http://localhost:8000/profs/video-feeds')
    return render(request, 'vids/video_list.html', {'feeds': feeds})

模板

{% for thing in feeds.entries %}
        <h1>Author</h1><br>
        {{thing.author}} <-- Nothing is printed here
        <h1>Title</h1>
        {{thing.title}}<br>
        <h1>Description</h1>
        {{thing.summary}}<br>
        <h1>Video Link</h1>
        <a href="{{thing.link}}">{{thing.title}}</a><br>
{% endfor %}


我讀了一點點到我提供的文檔,並注意到SyndicationFeed允許您添加的項目,其中的一個參數是author_name ,所以我換成get_context_data功能與item_author_name即返回功能item.author呯! 我是通過模板中的feeds.entries循環訪問它的(下面的新舊代碼可提供更好的上下文)

# Old Code
def get_context_data(self, item, **kwargs):
        context = super().get_context_data(**kwargs)
        context['author'] = item.author
        print(context['author'])
        return context 
# New Code
def item_author_name(self, item):
        print('Being Called')
        return item.author

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM