Почему текст модели не отображается в html?
Проблема в том, что не отображается текст описания_текста модели. Извините за мой английский
Это код html
{% for description_text in context_object_name %}
<h1 class="description"><a href="/Homepage/{{ goods.id }}/">{{goods.description_text}}</a></h1>
{% endfor %}
Это код файла views.py
class IndexView(generic.ListView):
template_name = 'Homepage/index.html'
model = Goods
context_object_name = 'goods.description_text'
def description(self):
return self.description_text
def price(self):
return self.price_text
def get_queryset(self):
"""Return the last five published questions."""
return Question.objects.order_by('-pub_date')[:5]
А это код файла models.py
class Goods(models.Model):
description_text = models.CharField(max_length=200)
price_text = models.CharField(max_length=200)
def __str__(self):
return self.description_text
def __str__(self):
return self.price_text
context_object_name
- это имя переменной шаблона, в которую передается список. Имена таких переменных не должны содержать точку (.
). Например, вы можете использовать:
class IndexView(generic.ListView):
template_name = 'Homepage/index.html'
model = Goods
context_object_name = 'goods'
В шаблоне вы затем перечисляете goods
и выводите description_text
для каждого good
:
{% for good in goods %}
<h1 class="description"><a href="/Homepage/{{ good.id }}/">{{ good.description_text }}</a></h1>
{% endfor %}