I Used Chain as in :
in views.py
places_list = Place.objects.all().order_by('-datetimecreated')[:5]
events_list = Event.objects.all().order_by('-datetimecreated')[:5]
photos_list = Photo.objects.all().order_by('-datetimecreated')[:5]
result_list = list(chain(photos_list, places_list, events_list))
In template file
{% for item in result_list %}
<a href="{% url view_place item.slug %}">{{item.title}}</a>
{% endfor %}
that’s to display a place but how to display an event or photo ?
<a href="{% url view_event item.slug %}">{{item.title}}</a>
<a href="{% url view_photo item.slug %}">{{item.caption}}</a>
thanks in advance .
I need to know a bit more to answer your question properly, but here are some ideas that might help you. I’m making the assumption that
photos_list,places_listandevents_listare all objects of different classes that are Django models.Option 1: Define a method on each class that determines the type of the object
For example, define a
content_typemethod on each model, like this:And then check this in your template:
Option 2: Define a
rendermethod on each classThis is probably much uglier, but you could define a
rendermethod on each object that returns the complete HTML you want to spit out for that object. Then it’s a case of doing this in your template:Sidenote: Consider inheritance
It sounds like photos, places and events are all things that could appear in the same feed, and as such they might share some common fields (like
posted_at). There are various things you need to consider with regard to this, so it’s probably best to look at the Django documentation on model inheritance.