【问题标题】:How are django variable tags used within html templates?django 变量标签如何在 html 模板中使用?
【发布时间】:2014-08-09 17:20:37
【问题描述】:

在编写 .html 文件时,变量标签“{{”“}}”在渲染时不会显示任何信息。 示例:

//model.py

 class model_name(models.Model):
     name_id = models.CharField(max_length=20, name='Name')
     link_id = models.CharField(max_length=4, name='Link')

//tables.py

class model_name_table(tables.Table):
    column_name = tables.TemplateColumn('<a href="{{model_name.link_id}}">{{model_name.name_id}}text_for_example</a>')

    class Meta:
        model = model_name
        fields = ('column_name', 'Name')

这会在渲染时产生“text_for_example”,但 {{model_name.link_id}} 或 {{model_name.name_id}} 都不显示任何内容。

简化为:

class model_name_table(tables.Table):
    column_name = tables.TemplateColumn('{{model_name.name_id}}')

    class Meta:
        model = model_name
        fields = ('column_name', 'Name')

在列中产生空白数据。变量标签应该如何用于渲染模型中的数据?

django-tables2 参考 http://django-tables2.readthedocs.org/en/latest/index.html#templatecolumn

djagno 模板语言参考https://docs.djangoproject.com/en/1.6/ref/templates/api/#django.template.Template

【问题讨论】:

    标签: python django django-tables2


    【解决方案1】:

    您链接的 django-tables2 文档准确显示了您出错的位置:包含当前行的上下文项称为 record,而不是 model_name。

    column_name = tables.TemplateColumn('<a href="{{record.link_id}}">{{record.name_id}}text_for_example</a>')
    

    【讨论】:

    • 谢谢。使用 record.link_id 和 record.name_id 不起作用,但是将其更改为 record.Name 和 record.Link 可以。
    猜你喜欢
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2016-11-21
    • 2013-05-12
    • 1970-01-01
    • 2018-03-19
    • 2018-12-20
    相关资源
    最近更新 更多