Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda