I’m pretty a newbie in Web-Development, so I’ve met some difficulties while trying to connect html-templates with Django code.
My plan for a certain page of my projected site is roughly to have a header on above, then lower there are 3 column-shaped containers (left to right, representing, i.e., different groups) filled with names of members of those groups (with vertical alignment, each member in a separate little container with photo, name and description).
I thought that, as I use Django, it would be stupid to hard-code all the members into containers, and there probably are some methods to fill those columns from the code. I used cycles.
Here is the template (“core” is a name of the app, templates are located in project/core/templates/core directory):
{% extends 'core/base.html' %}
{% block content %}
<h1>{{title}}</h1>
<div>Characters</div>
<div>
{% for fac in factions %}
{% if fac == outers %}
<div>
<h2>{{fac}}</h2>
{% for char in fac %}
<div>
<h3>{{char.name}}</h3>
<h3>{{char.description}}</h3>
</div>
{% endfor %}
</div>
{% endif %}
{% if fac == resistance %}
...
{% endfor %}
</div>
{% endblock %}
File core/models.py:
class Character(models.Model):
FACTION_CHOICES = (
('outers', 'Outer World'),
('resistance', 'Resistance'),
('neutrals', 'Neutral characters'),
)
name = models.CharField(max_length=100)
slug = models.SlugField(max_length=255, blank=True, db_index=True, default='')
faction = models.CharField('Faction', max_length=100, choices=FACTION_CHOICES, default='neutrals')
description = models.TextField(blank=True)
content = models.TextField(blank=True)
photo = models.ImageField(upload_to="photos/%Y/%m/%d/", blank=True)
time_create = models.DateTimeField(auto_now_add=True)
time_update = models.DateTimeField(auto_now=True)
objects = models.Manager()
def __str__(self):
return self.name
class Meta:
ordering = ['-time_create']
indexes = [models.Index(fields=['-time_create'])]
The corresponding view from core/views.py:
<...>
def characters(request):
chars = Character.objects.all()
faction_outers = Character.objects.filter(faction='outers')
faction_resistance = Character.objects.filter(faction='resistance')
faction_neutrals = Character.objects.filter(faction='neutrals')
factions = [faction_outers, faction_resistance, faction_neutrals]
data = {'title': "Characters", 'menu': menu, 'characters': chars, 'factions': factions}
return render(request, 'core/chars.html', context=data)
The separation of groups in several variables was made as the function regroup in the template wasn’t helpful, and I thought that problem is in functioning of regroup. But the method above didn’t help as well. Test server page in a browser shows in its code <div> " " == $0 </div>
on the place where the list of members must be located.
So, my question is – is there an opportunity to create and fill those container columns by cycles with data from QuerySets, and if there is, can you explain me how to do it properly?
If any additional info is necessary, I’m ready to provide it. Thanks a lot for your advices in advance.