我有3個應用程序,我想在主頁(index.html)上顯示他們的最新帖子。在django的主頁上顯示模型的最新帖子
Analizi models.py:
class Analiza(models.Model):
published = models.DateTimeField(default = datetime.now)
title = models.CharField(max_length = 500)
avtor = models.CharField(max_length = 200)
analiza_text = models.TextField(blank = True, null = True)
approved = models.BooleanField(default=False)
class Meta:
permissions = (
("can_approve_post", "Can approve post"),
)
def _unicode_(self):
return self.title
def get_absolute_url(self):
return "/%s/%s/%s/" % (self.published.year, self.published.month, self.slug)
其他兩(Recenzii和Lekcii)大多是相同的。
Analizi views.py:
def analizi(request):
post = Analiza.objects.order_by('-published')[:5]
return render_to_response('index.html', {'posts': post},)
但這個觀點我可以看到http://websiteurl.com/analizi結果(我知道這是錯誤的)。
如何顯示主頁上所有3個應用程序的最新帖子?在使用之後
def index(request):
posts = Analiza.objects.order_by('-published')[:5]
lektcii = Lektcii.objects.order_by('-published')[:5]
recenzii = Recenzii.objects.order_by('-published')[:5]
data = {'posts': posts, 'lektzii': lektzii, 'recenzii': recenzii}
render_to_response('index.html', data, context_instance=RequestContext())
他們在你index.html
: