So I have a simple template problem. In a URL view I pass one var, called ‘style’, and I pass a dictionary with a bunch of model info. To get right too it:
{{ style }}
{% for recipe in recipes %}
{{ recipe.recipe_style }}
{% if recipe.recipe_style == style %}
{{ recipe.recipe_style }}
{% endif %}
{% endfor %}
So what this block should return is:
Dinner Dinner Dinner Dinner Dinner
because there are only two entries in the recipe model, both with ‘Dinner’ set as the recipe_style (charfields), and the style is passed through the view and urlconf (“r’^(?P[-A-Za-z0-9_]+)/” which grabs ‘Dinner’ from a url ‘http://…/Dinner/’). What it returns is:
Dinner Dinner Dinner
This is because the if statement comes back false. But why? “Dinner” == “Dinner”! My only guess is a format issues? I tried style=str(style) before passing it in the view, but still didn’t work.
EDIT: here’s what shows in the shell:
>>> RecipeStyle.objects.all()
[<RecipeStyle: Dinner>, <RecipeStyle: Lunch>, <RecipeStyle: American>, <RecipeStyle: Italian>, <RecipeStyle: French>]
>>> recipe = Recipe.objects.get(pk=1)
>>> recipe.recipe_style
<RecipeStyle: Dinner>
and it works in the console:
>>> if recipe.recipe_style == RecipeStyle.objects.get(pk=1):
... print "poop"
...
poop
So why not in the template?
I was doing it wrong. The if statement I had was filtering a queryset passed to the template; dumb, right? I fixed it by filtering the recipe model in the view and then passing the filtered queryset.