我不知道为什么我直接使用标签获取HTML内容。其余代码没问题,但内容没有。
def ShowContent(content):
plantilla = get_template('index.html')
c = Context({"content": content})
renderizado = plantilla.render(c)
return renderizado
def ShowNextTen(request):
NextActQuery = Actividad.objects.all().order_by('fecha')
NextAct = "Listado actividades<br>"
for Act in range(0,10):
NextAct += '<div class="activity">'
NextAct += '<strong>' + NextActQuery[Act].titulo + '</strong><br>'
NextAct += NextActQuery[Act].tipo_evento + "<br>"
NextAct += NextActQuery[Act].fecha.strftime("%d-%m-%y") + "<br>"
NextAct += '</div>'
return HttpResponse(ShowContent(NextAct))
答案 0 :(得分:0)
答案 1 :(得分:0)
您的NextVar内容应该在模板中,在您的情况下,它可能是index.html,它应该扩展包含您当前index.html包含的内容的通用模板。在python中编写普通的html并不是django的设计方式,因此不是最佳方式。
然后,我会使用ListView(如果您希望能够显示接下来的10个条目,则使用Paginator)。您可能甚至不需要在views.py中添加一行。
from django.views.generic.list import ListView
...
url(r'^actividades/$', ListView.as_view(
template_name="index.html",
queryset=Actividad.objects.all().order_by('fecha')[:10],
context_object_name="actividades")
)),
...