Every variable makes it to index.html except for one?
I'm doing a django project (I'm new to django). So far everything has been running smoothly except for one issue that I can't seem to figure out.
Here's my get method for Django:
class Index(TemplateView):
template_name = 'project/index.html'
def get(self, request):
allBrands = InventoryItem.objects.values_list('brand', flat=True).distinct().order_by('totalReviews')
allAgeGroups = InventoryItem.objects.values_list('ageGroup', flat=True).distinct()
items = InventoryItem.objects.all()
return render(request, self.template_name, {
'items': items,
'allBrands': allBrands,
'allAgeGroups': allAgeGroups,
})
When I added 'allAgeGroups'
I was running into the issue where for some reason the index.html
was not receiving the information.
The query works.
When I print(allAgeGroups)
in the get()
function, I get nothing
When I print(allAgeGroups)
in the post()
function, I get <QuerySet ['Adult', 'Youth']>
(what I want)
And I just realized I can remove everything from the render function, save the file, refresh the page, and everything still works???
What is happening?
Thank you.
You are calling the same model three times, although it seems unnecessary. It’s better to try something like this:
views.py
class Index(TemplateView):
template_name = 'project/index.html'
def get(self, request):
items = InventoryItem.objects.all()
return render(request, self.template_name, {
'items': items,
})
index.html
{% extends "project/base.html" %}
{% block content %}
{% include "project/nav.html" with items=items %}
{% for item in items %}
<div>{{ item.ageGroup }}</div>
<div>{{ item.brand }}</div>
{% endfor %}
{% endblock %}
And don’t forget to register your TemplateView in urls.py
.