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,
)
-
I believe what you are following is the only solution.Anup Yadav– Anup Yadav2017年12月23日 05:59:23 +00:00Commented Dec 23, 2017 at 5:59
-
@AnupYadav, sorry, I didn't understand what you meant.GuiFGDeo– GuiFGDeo2017年12月23日 06:01:56 +00:00Commented Dec 23, 2017 at 6:01
-
can you show what context variable you are passing from your view?Umar Asghar– Umar Asghar2017年12月23日 06:09:58 +00:00Commented Dec 23, 2017 at 6:09
-
The dictionary being passed to the template doesn't contain "section". Did you mean "sections"?Kent Shikama– Kent Shikama2017年12月23日 06:10:08 +00:00Commented Dec 23, 2017 at 6:10
-
@UmarAsghar, isnt the context variable the one in bold on my first comment?GuiFGDeo– GuiFGDeo2017年12月23日 06:13:36 +00:00Commented Dec 23, 2017 at 6:13
1 Answer 1
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.