Answer the question
In order to leave comments, you need to log in
How to split received data from many-to-many model when displaying in html?
Good day The
question is, how can I separate the received data when outputting to html, or how can I display them separately?
My models:
class Variety(models.Model):
varietyName = models.CharField(max_length=30)
def __str__(self):
return self.varietyName
class Fruits(models.Model):
name = models.CharField(max_length=255)
variety = models.ManyToManyField(Variety)
def __str__(self):
return self.name
def index(request):
return render(request, 'index.html', {'fruits': Fruits.objects.all().order_by('-id')[:1]})
{% for fruit in fruits %}
<h5>{{ fruit.name }}</h5> // название фрукта
{% for v in fruit.variety.all %}
<h5>{{ v.varietyName }}</h5> // название вида
{% endfor %}
{% endfor %}
{{v.varietyName}}, but I need to place each received value
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question