Reputation: 43
Can't pass extra variable with listview
I tried adding another function and returning the value but it then doesn't return the main part.
class PostListView(ListView):
model = Post
template_name = 'blog/home.html' # <app>/<model>_<viewtype>.html
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 3
def get_context_data(self, **kwargs):
posting = []
for post in Post.objects.all():
post_words = post.content.split()
for word in post_words:
posting.append(word.lower())
mostused_word = []
for word in posting:
if len(mostused_word) == 0:
mostused_word.append({'daword': word, 'word_count': posting.count(word)})
else:
if posting.count(word) > mostused_word[0]['word_count']:
mostused_word[0] = {'daword': word, 'word_count': posting.count(word)}
context = {
'mostused_word': mostused_word[0]['daword'],
'mostused_word_count': mostused_word[0]['word_count'],
'postin': posting,
}
return context
I expect to pass both needed variables, not only one of them.
Upvotes: 4
Views: 793
Reputation: 599628
You need to call the super method.
def get_context_data(self, **kwargs):
...
context = {
'mostused_word': mostused_word[0]['daword'],
'mostused_word_count': mostused_word[0]['word_count'],
'postin': posting,
}
kwargs.update(context)
return super().get_context_data(**kwargs)
Upvotes: 4