Search code examples
pythondjangopython-3.xdjango-2.1

Django, my for loop is not showing in include template


Hello I am a beginner with Django and Python. I am currently in my project using a for loop in the template but it does not show anything. Could someone help me and explain me what I am doing wrong?

models.py

class ImageCategory(models.Model):
    name = models.CharField(blank=False, max_length=120)
    created_at = models.DateTimeField(default=datetime.now(), blank=True)

    class Meta:
        verbose_name_plural = "image categories"

    def __str__(self):
        return self.name

views.py

from .models import ImageCategory

def HomeView(request):
template = loader.get_template('editor.html')

return HttpResponse(template.render())


def LibraryOverviewView(request):  
    return render(request, 'library_overview.html', {'image_categories': ImageCategory.objects.all()})

So I put the category information in the libraryOverView, but the editor.html is using HomeView. library_overview.html is included in another html called editor.html

editor.html

section class="toolbox document-tools">
              <ul>
                <li class="tb-title">Document</li>
                <!-- uncomment to see all available styles -->
                <!--
                <li class="tb-btn tb-btn-big tb-btn-disabled">Preview</li>
                -->
                <li class="tb-btn tb-btn-big" id="btn-export">Export</li>
                <li class="tb-btn tb-btn-big tb-btn-action">Save</li>
              </ul>
            </section>

          </span>

          {% include 'library_overview.html' %}

          {% include 'library_categories/colorful_images.html' %}

          {% include 'library_categories/colorful_images_categories/blue_images.html' %}

        </span>

library_overview.html

{% for category in image_categories %}
     <a class="tb-btn tb-btn-label tb-btn-radio no-bg slide-forward">-> {{ category.name }}</a>
 {% empty %}
     <p> There are no Categories yet </p>
 {% endfor %}

urls.py

urlpatterns = [url(r'^library_overview/', views.LibraryOverviewView, 
    name='LibraryOverviewView'),

Solution

  • The editor.html is using the HomeView, and not LibraryOverviewView. But the html for libraryOverviewView was included inside editor.html, thus still using the HomeView. Moving the information from LibraryOverView to HomeView worked.