为什么模型文本没有显示在 html 中?

Why model text is not displayed in html?

问题是模型description_text的文字没有显示。对不起我的英语。

这是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

这是admin.py

的代码
from django.contrib import admin
from .models import Good
from .models import Question

admin.site.register(Good)

admin.site.register(Question)


class Good(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['description_text']}),
        (None, {'fields': ['price_text']}),
    ]

context_object_name 是用于将列表传递到的模板变量的名称。这样的变量名不应该有一个点(.)。例如,您可以使用:

class IndexView(generic.ListView):
    template_name = 'Homepage/index.html'
    model = Goods
    <strong>context_object_name = 'goods'</strong>

在模板中,然后枚举 goods 并为每个 good:

渲染 description_text
{% for <strong>good in goods</strong> %}
     <h1 class="description"><a href="/Homepage/{{ good.id }}/">{{ <strong>good.description_text</strong> }}</a></h1>
{% endfor %}

您构建的 ModelAdmin 注册。实际上,您注册了 Good 模型,但没有使用 ModelAdmin,您需要将 ModelAdmin 和 link 定义为 Good 模型:

from django.contrib import admin
from .models import Goods, Question

class <strong>GoodAdmin</strong>(admin.ModelAdmin):
    fieldsets = [
        (None, {'fields': ['description_text', 'price_text']}),
    ]

# link Goods to the GoodAdmin
admin.site.register(Goods, <strong>GoodAdmin</strong>)
admin.site.register(Question)