GuiFGDeo
GuiFGDeo

Reputation: 736

Issue with Django Template for loop

so I did some searches but couldn't find the answer to my particular issue... What is happening is that:

I have a model named section which have section_title field. This field can have spaces, for example: "First Section".

When I pass this to Django, I am removing the space in Python with replace(), and created a filter on Django which also removes the space like so:

@register.filter(name='replace_char')
def replace_char(value, arg):
    return value.replace(arg, '')

Then on my template:

{% for subsection in section.section_title|replace_char:" " %}

The issue is that subsection is being shown as each character from section_title, instead of the list that section_title references. Here is the dictionary being passed to the template:

{'sections': [< Section: First Section>, < Section: Second Section>], 'FirstSection': [< Subsection: Subsection 1>, < Subsection: Subsection 2>], 'SecondSection': [< Subsection: Bla1>], 'teste': ['1', '2', '3']}

If I hardcode:

{% for subsection in FirstSection %}

It works...

Any ideas? Thanks!

OBS: I removed the spaces because I thought they were causing the issue, but apparently not. It wasnt working with the spaces as well...

Full template code:

{% for section in sections %}
    <div class="sectionHeader">
        {{ section.section_title }}
    </div>
    <div class="forumSection">
    {% for subsection in section.section_title|replace_char:" " %}
        <div>
            {{ subsection }}
        </div>
    {% endfor %}
    </div>
{% endfor %}

Models:

class Section(models.Model):
    def __str__(self):
        return self.section_title
    section_title   = models.CharField(primary_key = True, unique = True, max_length = 50)
class Subsection(models.Model):
    def __str__(self):
        return self.subsection_title
    subsection_title    = models.CharField(max_length = 50)
    subsection_section  = models.ForeignKey(
                        'Section',
                        on_delete = models.CASCADE,
    )

Upvotes: 0

Views: 67

Answers (1)

Kent Shikama
Kent Shikama

Reputation: 4060

You want to set up a related_name on your foreign key so that you can fetch all your subsections corresponding to the section.

class Section(models.Model):
    section_title   = models.CharField(primary_key = True, unique = True, max_length = 50)
class Subsection(models.Model):
    subsection_title    = models.CharField(max_length = 50)
    subsection_section  = models.ForeignKey(
                        'Section',
                        related_name = 'subsections',
                        on_delete = models.CASCADE,
    )

And then you can change your template code to loop over your subsections as follows:

{% for section in sections %}
    <div class="sectionHeader">
        {{ section.section_title }}
    </div>
    <div class="forumSection">
        <div>
            {% for subsection in section.subsections.all %}
                {{ subsection.subsection_title }}
            {% endfor %}
        </div>
    </div>
{% endfor %}

See https://docs.djangoproject.com/en/1.11/topics/db/queries/#related-objects for more information.

Upvotes: 1

Related Questions