Reputation: 45
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.
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. the debug print is print(context['author'])
which returns as expected everytime the feed is interacted with.
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})
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 %}
Upvotes: 0
Views: 227
Reputation: 45
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! I accessed it through the feeds.entries
loop in the template (old and new code below for better context)
# 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
Upvotes: 0