Search code examples
pythondjangofeed

Something like publish notes in a webpage


I'm building a site where I publish a notice and it appear in one webpage but my code dosen't return the objects. I post my code then:

Models:

from django.db import models

class Contenidos(models.Model):
    title = models.CharField(max_length = 255)
    content = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    published = models.BooleanField(default=True)


    class Meta:
        ordering = ['-created']

    def __unicode__(self):
        return u'%s' % self.title

Views:

from django.shortcuts import render
from granada.models import Contenidos

def contenidos(request):
    contenido_list = Contenidos.objects.all()
    return render(request, 'contenidos.html', {'contenido_list' : contenido_list})

contenidos.html

{% extends 'base.html' %}

{% block title %} Contenidos {% endblock %}

{% block content %}
    <h2> contenidos </h2>

    {% for contenido in contenido_list %}
        <h3>
            {{ contenido_list.title }}
        </h3>
        <p>{{ contenido_list.content }}</p>
    {% endfor %}
{% endblock %}

Solution

  • You're accessing the wrong object in your loop, contenidos instead of contenido.

    {% extends 'base.html' %}
    
    {% block title %} Contenidos {% endblock %}
    
    {% block content %}
        <h2> contenidos </h2>
    
        {% for contenido in contenidos %}
            <h3>
                {{ contenido.title }}  {# was contenidos.title #}
            </h3>
            <p>{{ contenido.content }}</p>
        {% endfor %}
    {% endblock %}