user4532393
user4532393

Reputation:

How to render queryset in django template

Hi Im using this code for rendering

def ShowContent(dic, htmlpage):
    plantilla = get_template(htmlpage)
    c = Context(dic)
    renderizado = plantilla.render(c)
    return HttpResponse(renderizado)

and I wanna represent this Query, so I use 'AllQuery' dict for 'todas.html'.

def ShowAll(request):
    AllQuery = Actividad.objects.all().order_by('fecha')
    print AllQuery
    return ShowContent(AllQuery, 'todas.html')

I don't know how to represent it in my template

                    <tr>
                        <td></td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                        <td> </td>
                    </tr>

Actividad from models.py

 class Actividad(models.Model):
    id_evento = models.IntegerField(primary_key=True)
    titulo = models.CharField(max_length=100)
    tipo_evento = models.CharField(max_length=50)
    gratis = models.BooleanField(default=False)
    fecha = models.DateField()
    hora = models.TimeField()
    largo = models.BooleanField(default=False)
    url = models.URLField(max_length=128)

any idea? I think its easy but I'm not gettin it. I'm trying this:

                            {% for i in AllQuery %}
                                { AllQuery.i.titulo }
                            {% endfor %}

from views, I can access doing

def ShowAll(request):
    AllQuery = Actividad.objects.all().order_by('fecha')
    print AllQuery[0].titulo

Upvotes: 1

Views: 4218

Answers (1)

Pieter Hamman
Pieter Hamman

Reputation: 1612

Just use a generic list view, this will make your life a lot easier and save some time.

class ListView(generic.ListView)
    model=Actividad
    template_name='todas.html'

and then in your template you can display the list like so

{% for i in object_list %}
    {{ i.titulo }}
{% endfor %}

Upvotes: 4

Related Questions