Can I make my template syntax simpler? I'm hoping to eliminate the if
and maybe also the for
block.
This worked in the shell but I can't figure out the template syntax.
recipes[0].recipephotos_set.get(type=3).url
model.py
class Recipe(models.Model):
....
class RecipePhotos(models.Model):
PHOTO_TYPES = (
('3', 'Sub Featured Photo: 278x209'),
('2', 'Featured Photo: 605x317'),
('1', 'Recipe Photo 500x358'),
)
recipe = models.ForeignKey(Recipe)
url = models.URLField(max_length=128,verify_exists=True)
type = models.CharField("Type", max_length=1, choices=PHOTO_TYPES)
view.py
recipes = Recipe.objects.filter(recipephotos__type=3)
template.html
{% for recipe in recipes %}
{% for i in recipe.recipephotos_set.all %}
{% if i.type == '3' %}
{{ i.url }}
{% endif %}
{% endfor %}
<a href="/recipe/{{ recipe.recipe_slug }}/">{{ recipe.recipe_name }}</a></li>
{% empty %}