22👍
✅
This is my working answers:
{% for entry in entries %}
{% if entry.category == 'General Member' %}
<a href="{% url 'member:person-list' %}"><li>{{ entry.category }}</li></a>
{% elif entry.category == 'Executive Committee Member' %}
<a href="{% url 'member:execomember-list' %}"><li>{{ entry.category}}</li></a>
{% else %}
<a href="{% url 'member:person-list' %}"><li>{{ entry.category}}</li></a>
{% endif %}
{% empty %}
<li>No recent entries</li>
{% endfor %}
Webpage view of output:
For more clarifications, I check my code with the django shell. See the snippet of my shell:
Even I change the order of if conditions, result remain the same. See my shell code with output:
Do you see any wrong with my codes? Its fully comply with the python conditions and gives expected results. Anybody can check it on their django shell.
👤ohid
1👍
From what I understand, you want to associate each entry to a url depending on the category it belongs among the 3 categories. you can have this refactored in your Entry
model in order to minimize logic in templates like:
class Entry(models.Model):
category = ...
def get_entry_url(self):
if self.category == 'General Member':
return reverse for the url 'member:person-list'
elif self.category == 'Executive Committee Member':
return reverse for the url 'member:execomember-list'
else:
return reverse for the url 'member:person-list'
Then in template:
{% for entry in entries %}
<a href="{{ entry.get_entry_url }}"><li>{{ entry.category }}</li></a>
{% empty %}
<li>No recent entries</li>
{% endfor %}
- [Django]-How do I send XML POST data from an iOS app to a Django app?
- [Django]-Django Humanize naturaltime templatetag only partly translating
- [Django]-Relative font URLs in CSS cause 403s on S3
- [Django]-How can I display an jupyter notebook on a homepage built with django?
- [Django]-Default Django Admin Forms and FormWizard
Source:stackexchange.com