Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

199
Vistas
Django objects.all() doesn`t display any content

Hi everybody! Im just starting a way of django programming so sometimes really get confused.

I`m trying to display all my objects from DB, but when opening the page its simply empty.

There are content added and I tried ListView previously and it worked for me. But now I need to dislpay objects like a grid and here is an issue with this method.

Will be very thanksfull for any help!

models.py

from django.db import models


class Post(models.Model):
    title = models.CharField(max_length=140)
    body = models.TextField()
    date = models.DateField()
    image = models.ImageField(upload_to='bons_images/%Y/%m/%d')

    def __str__(self):
        return self.title

views.py

from django.shortcuts import render, render_to_response
from django.template import RequestContext
from django.views import generic

from blog.models import Post


def image(request):
    post = Post()
    variables = RequestContext(request, {
        'post': post
    })
    return render_to_response('blog/post.html', variables)


# class IndexView(generic.ListView):
#     template_name = 'blog/blog.html'
#     context_object_name = 'all_posts'
#
#     def get_queryset(self):
#         return Post.objects.all()

def index(request):
    posts = Post.objects.all()
    return render(request, 'blog/blog.html', {'posts': posts})

urls.py

from django.conf.urls import url, include
from django.views.generic import ListView, DetailView
from blog.models import Post
from blog import views

urlpatterns = [
    url(r'^$', views.index, name='index'),
    url(r'^(?P<pk>\d+)$', DetailView.as_view(model=Post, template_name='blog/post.html')),
]

blog.html

{% extends 'base.html' %}

{% block content %}

    {% if all_posts %}
        {% for post in all_posts %}
            <div class="container-fluid">
                <div class="row">
                    <div class="col-sm-3 col-lg-4">
                        <div class="thumbnail">
                            <a href="/blog/{{ post.id }}">
                                <h5>{{ post.date|date:'Y-m-d' }} {{ post.title }}</h5>
                                <img src="{{ post.image.url }}" style="width: 50%; height: 50%"/>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        {% endfor %}
    {% endif %}
{% endblock %}

And by the way, how its possible to display your objects like in grid, not list, using Bootstrap or so on.

Thank you!

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You're iterating over something called all_posts in your template. But your view doesn't send anything called all_posts; it only sends posts. You need to use consistent names.

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda