Joe Kasedde Joe Kasedde - 2 months ago 11
Python Question

My Django application is not displaying content from the db

{% extends 'photos/base.html' %}
{% block galleries_active %}active{% endblock %}

{% block body%}
<div class="gallery-container" container-fluid>

<!--- Galleries--->
<div class="row">
<div class="col-sm-12">
<h3>Text's Gallery</h3>
</div>
{% if gallery %}
{% for gallery in galleries %}
<div class="col-sm-4 col-lg-2">
<div class="thumbnail">
<a href="{% url 'photos:details' gallery.id %}">
<img src="{{gallery.Gallery_logo}}" class="img-responsive" height="240" width="240">
</a>
<div class="caption">
<h2>{{gallery.Title}}</h2>
<h4>{{gallery.Category}}</h4>

<!-- View Details-->
<a href="{% url 'photos:detail' gallery.id %}" class="btn btn-primary btn-sm" role="button">View Details</a>

<!-- Delete Album-->
<form action="{% url 'photos:delete_gallery' gallery.id %}" method="post" style="display: inline;">
{% csrf_token %}
<input type="hidden" name="gallery_id" value="{{gallery.id}}">
<button type="submit" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-trash"></span>
</button>
</form>

<!-- Favorite -->
<a href="#" class="btn btn-default btn-sm btn-favorite" role="button">
<span class="glyphicon glyphicon-star"></span>
</a>
</div>
</div>
</div>
{% cycle '' '' '' '' '' '<div class="clearfix visible-lg"></div>' %}
{% endfor %}
{% else %}
<div class="col-sm-12">
<br>
<a href="#">
<button type="button" class="btn btn-success">
<span class="glyphicon glyphicon-plus"></span>&nbsp; Add a Gallery
</button>
</a>
</div>
{% endif %}

</div>

</div>

</div>

{% endblock %}


That is my index file

below is my views.py file

from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from .models import Gallery

class IndexView(generic.ListView):
template_name = 'photos/index.html'
context_object_name = 'all_galleries'

def get_queryset(self):
return Gallery.objects.all()

class DetailsView(generic.DetailView):
model = Gallery
template_name = 'photos/detail.html'

class GalleryCreate(CreateView):
model = Gallery
fields = ['Title','Category','Gallery_logo']

class GalleryUpdate(UpdateView):
model = Gallery
fields = ['Title','Category','Gallery_logo']

class GalleryDelete(DeleteView):
model = Gallery
success_url = reverse_lazy('photos:index')


and my models.py

from django.db import models
from django.core.urlresolvers import reverse


class Gallery(models.Model):
Title = models.CharField(max_length=250)
Category = models.CharField(max_length=250)
Gallery_logo = models.CharField(max_length=1000)

def get_absolute_url(self):
return reverse('photos:detail', kwargs={'pk': self.pk})

def __str__(self):
return self.Title + '_' + self.Gallery_logo


class Picture (models.Model):
Gallery = models.ForeignKey(Gallery, on_delete=models.CASCADE)
Title = models.CharField(max_length=250)
Artist = models.CharField(max_length=250)
Price = models.CharField(max_length=20)
interested = models.BooleanField(default=False)

def __str__(self):
return self.Title


I was following the newboston django tutorials video number 30 trying to make the same out put as bucky. If any one has followed the series and has a clean version of the video 30 index page source code please help out.

I modified the code to work for an online photogallery store where the admin will add images which are grouped in categories which can be downloaded by visitors.

Answer

You have context_object_name = 'all_galleries' in

class IndexView(generic.ListView):
    template_name = 'photos/index.html'
    context_object_name = 'all_galleries'
    ...

However you loop over galleries in template

{% for gallery in galleries %}

Needs to be

{% for gallery in all_galleries %}

Also you have {% if gallery %} before the loop in template which doesn't make sense, because there is no gallery variable. You need to check {% if all_galleries %}.

NOTE #1: your field names in classes that in models better be lowercase.

NOTE #2: in IndexView you need to provide model and you can remove get_queryset(), because there is no custom query that retrieve data with filters. So you need to use

class IndexView(generic.ListView):
    model = Gallery
    template_name = 'photos/index.html'
    context_object_name = 'all_galleries'