Для проекта с домашним животным я пытаюсь отобразить посты и картинки, отсортированные по дате создания.
Прямо сейчас я могу отображать их в отсортированном порядке, но отдельно (например, сначала сообщения в отсортированном порядке, а затем изображения в отсортированном порядке).
Я хотел бы показать их на стене в зависимости от даты создания (независимо от того, является ли это публикацией или рисунком).
Models.py
class AddStatus(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE )
status = models.TextField()
date = models.DateTimeField(default=datetime.now)
class ImageLib(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE )
picture = models.ImageField()
date = models.DateTimeField(default=datetime.now)
Views.py
class ProfilePage(ListView):
model = models.UserCreation
context_object_name = 'profile'
def get_context_data(self, *args, **kwargs):
context = super(ProfilePage, self).get_context_data(*args,
**kwargs)
user =self.request.user
try:
context['data'] = ProfileData.objects.get( user=user)
context['add'] = AddStatus.objects.filter(user=user).order_by('-date')
context['uploadpic'] = ImageLib.objects.filter(user=user).order_by('-date')
except ProfileData.DoesNotExist:
context['data']= None
context['add']=None
context['uploadpic'] = None
return context
Template
{% for items in add %}
<h6 class="display-5" style="font-weight:1px"><i class="fas fa-plus-square"></i> <strong class="capitalize">{{items.user.usercreation.fname}} {{items.user.usercreation.lname}}</strong><span style="font-size:16px; font-weight:1px"> wrote "{{items.status}}"</span>
<small style="font-size:0.7rem"> {{items.date.date}} at {{items.date.time}}</small><a style="float:right" href="{% url 'usercreation:deletestatus' items.pk %}"><i class="fas fa-trash"></i></a></h6>
<hr>
{% endfor %}
{% for pics in uploadpic %}
<h6 class="display-5" style="font-weight:1px"><i class="fas fa-plus-square"></i> <strong class="capitalize">{{pics.user.usercreation.fname}} {{pics.user.usercreation.lname}}</strong><span style="font-size:16px; font-weight:1px"> uploaded a picture</span>
<small style="font-size:0.7rem"> {{pics.date.date}} at {{pics.date.time}}</small><a style="float:right" href="{% url 'usercreation:deletepic' pics.pk %}"><i class="fas fa-trash"></i></a></h6>
<img style="width:300px" src="{{pics.picture.url}}" alt="">
<hr>
{% endfor %}