Search code examples
pythondjangodjango-templatesforeign-keys

Django Using conditional in template based on foreginKey value?


I'm wondering if you can use conditions to change what is shown from the template based on a foreignKey. On my project, users will choose a muscle group they want to workout (Chest, Back, Legs, etc.), meaning each workout has a foreginKey to a large muscle group.

I want to make it so if a user chooses a workout that has a foreignKey to Legs, that the template will be unique vs. if the workout was a Chest exercise.

Any way to do this?


Solution

  • You could use {% if muscle_part == [ENTER_STRING_TO_VERIFY_WITH] %} in your HTML and pass the muscle part string in your context while rendering the view. This way all your programs would be in one template but based on your context, they will be displayed accordingly.