简体   繁体   English

如何在get_context_data RSS Feed django 2.2中访问数据

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

I'm trying to access the context dictionary that is returned by a modified 'get_context_data' function in the LatestVideosFeed so I can use it in a 'news feed' I'm trying to make because the context that is returned contains the author of the video. 我试图访问受在修改“get_context_data”函数返回的语境词典LatestVideosFeed这样我就可以在“新闻提要”我试图让使用它,因为返回的上下文包含的作者视频。 I've been following these docs https://docs.djangoproject.com/en/2.2/ref/contrib/syndication/ and I can't figure out how to access the context dictionary that returns from the get_context_data(self, item, **kwargs): It works and when I do a debug print just before the return it returns a dictionary with the last entry as the user that uploaded the video. 我一直在关注这些文档https://docs.djangoproject.com/en/2.2/ref/contrib/syndication/ ,但我不知道如何访问从get_context_data(self, item, **kwargs):它可以工作,当我在返回之前进行调试打印时,它会返回一个字典,其中最后一个条目是上载视频的用户。 the debug print is print(context['author']) which returns as expected everytime the feed is interacted with. 调试打印为print(context['author']) ,每次与提要进行交互时,它都会按预期返回。

feeds.py 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 views.py

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

template 模板

{% 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 %}


I read a little bit more into the docs I provided and noticed that the SyndicationFeed allows you to add items, one of the parameters was author_name , so I replaced get_context_data function with an item_author_name function that returned item.author and Boom! 我读了一点点到我提供的文档,并注意到SyndicationFeed允许您添加的项目,其中的一个参数是author_name ,所以我换成get_context_data功能与item_author_name即返回功能item.author呯! I accessed it through the feeds.entries loop in the template (old and new code below for better context) 我是通过模板中的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