Arekay Arekay - 4 months ago 21
HTML Question

render django tag inside textfield written in html

im a beginner in django and general programming and would like to ask a questions regarding how to render django model field in html save inside textfield.

my code snippet as per below:

models.py

class Recipe(models.Model):
recipe_name = models.CharField(max_length=128)
recipe_text = models.TextField()
ingredients = models.TextField()

def __str__(self):
return self.recipe_name


I have ingredient model which contains object for ingredients for example sugar or salt.

class ingredient(models.Model):
ingredient_name = models.CharField(max_length=200)
ingredient_text = models.TextField()

def __str__(self):
return self.ingredient_name


For example if i create salt ingredient object with the ingredient_name of 'salt', i want to call the ingredient_name inside instantiated Recipe object, ingredients field using the ul list html code save inside it using form and pass the code to the template using autoescape or safe tag. but it doesnt seem to work for the field.
The html work for ul list, but the content doesnt seems to work . it will only load for example a string of {{ ingredients.0.ingredient_name }}

i pass both the recipe objects and ingredient objects in views.py

is there any other way to do this?

Answer

You need to link Recipe to Ingredients:

class Ingredient(models.Model):
    ingredient_name = models.CharField(max_length=200)
    ingredient_text = models.TextField()

    def __str__(self):
        return self.ingredient_name

class Recipe(models.Model):
    recipe_name = models.CharField(max_length=128)
    recipe_text = models.TextField()
    ingredients = models.ManytoMany(Ingredient)

    def __str__(self):
        return self.recipe_name

Then, create your ingredient, like this:

salt = Ingredient(ingredient_name='salt', ingredient_text='as per taste')
salt.save()

chips = Ingredient()
chips.ingredient_name = 'Chips'
chips.ingredient_text = 'Delicious, goes well with salt'
chips.save()

Next, add it to a recipe:

recipe = Recipe()
recipe.recipe_name = 'Salty Chips'
recipe.recipe_text = 'Great for parties'
recipe.save() # You have to save it first

recipe.ingredients_set.add(salt)
recipe.ingredients_set.add(chips)

recipe.save() # Save it again

Now, in your view:

def show_recipe(request):
    recipes = Recipe.objects.all()

    return render(request, 'recipe.html', {'recipes': recipes})

Finally, in your template:

{% for recipe in recipes %}
   {{ recipe.recipe_name }}
   <hr />
   Ingredients:
   <ul>
   {% for ingredient in recipe.ingredients_set.all %}
      <li>{{ ingredient }}</li>
   {% endfor %}
   </ul>
{% endfor %}

This works because you have created a relationship between your Recipe and Ingredient model in such a way that each Recipe can have one or more Ingredient objects linked to it.

Django will keep track of the relations for you, and using the model api you can add (and remove) ingredients to any recipe object.

Since the relations are managed for you, whenever you have a Recipe object, it knows all the Ingredient objects that are linked to it; and we can easily print the correct recipe.

Comments