Skip to content

Instantly share code, notes, and snippets.

@varqasim
Last active August 29, 2015 14:27
Show Gist options
  • Save varqasim/8c7df5c17635e60c3027 to your computer and use it in GitHub Desktop.
Save varqasim/8c7df5c17635e60c3027 to your computer and use it in GitHub Desktop.
How a template would look like when using two models in a Django ListView http://stackoverflow.com/questions/12187751/django-pass-multiple-models-to-one-template
{% for post in posts %}
<a href="{% url 'post' post_name_slug=post.slug %}"></a>
{% endfor %}
{% for post_comments in comments %}
{{ post_comments }}
{% endfor %}
class IndexView(ListView):
context_object_name = 'index'
queryset = Post.objects.all()
template_name = 'index.html'
def get_context_data(self, **kwargs):
context = super(IndexView, self).get_context_data(**kwargs)
context['posts'] = Post.objects.all()
context['comments'] = Comment.objects.all()
return context
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment