In one of my Django projects in views.py I've got some code:
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
# Create your views here.
products = Product.objects.all()
product_list = list(products)
def displayhome(request):
return render(request, 'bootstrap/index.html', {'title': product_list[0].title}, {'link': product_list[0].official_path_name})
Now using this (very clunky) method, I am able to put the string version of variables into html using:
{{title}}
for example. However this does not let me operate on the variables, e.g. if I sent through product_list I couldn't get product_list[0]. I vaguely know of using {% %} tags instead of {{ }} but I am (a) not completely sure how they work and (b) don't know how powerful they are e.g. if you could use it like a normal python file.
How would I use a variable from python (say it is an integer with value 4) to create a variable number of html elements (e.g. 4 boxes) for example?
If there is no easy way to execute python in html, is there a way to instead put my python variables into javascript and then somehow use those in the html instead?
.operator, no matter what datastructure it is, so what you want isproduct_list.0product_list[0]in the template you have to write it as{{ product_list.0 }}.