django - 为什么 {{ 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"
解决方案
属性为label_tag
,带有下划线。
推荐阅读
- asp.net-mvc - 如何在 ASP.NET MVC 5 中从相关表中添加两个值
- pandas - 在条件下替换熊猫列下的值
- python - python panda:如何从csv文件中删除行
- javascript - 从 Javascript 中的现有数组重新创建一个数组
- r - 识别 R 条件语句中的空白
- javafx - 内部带有可拖动节点的 JavaFX 平移和缩放
- javascript - FormControl 如何微调错误验证消息以更具体
- android - 如何在阅读模式下使用 webView kotlin?
- java - 创建 ListNode 的新实例
- java - 当 JUnit 中的实际值和期望值相同时,为什么测试会失败?