首页 > 解决方案 > 为什么 {{ attribute.label.tag }} 中的“label.tag”在 django 模板中不起作用?我们如何获取模板中列的标签?

问题描述

可以从 django 模板中的 views.py 中获取值。获取标签然后获取其值是行不通的。得到它的方法是什么?

在模块.py

class Article(models.Model):
    pub_date = models.DateField()
    headline = models.CharField(max_length=200)
    content = models.TextField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
    def __str__(self):
        return self.headline

在views.py中

def articlesindex(request):    
    data = Articles.objects.all().last()
    return render(request, 'Articles\index.html', {'articles':data}

在 index.html 中

{{ articles.headline }} //gives you the value
{{ articles.headline.label_tag }} //does not give you the name "headline"

标签: djangodjango-templates

解决方案


属性为label_tag,带有下划线。


推荐阅读