【问题标题】:Overriding Crispy Forms template fails覆盖 Crispy Forms 模板失败
【发布时间】:2018-09-17 00:28:48
【问题描述】:

我正在尝试覆盖 Crispy Forms 1.7.0 中选项卡的默认模板。作为第一步,我尝试将相关模板 (templates/bootstrap3/layout/tab-link.html) 从清晰的表单模板复制到我的应用程序模板目录 (myapp/templates/crispy_overrides/tab-link.html) 但是当我尝试时它停止正确呈现选项卡在布局中使用我的目录中的模板。

以下是我的表单布局对象和模板的相关部分。如果我注释掉 template=... 行,一切正常。

我做错了吗?

TabHolder(
    Tab('D.DDD°',
        'latitude',
        'longitude',
        template='crispy_overrides/tab-link.html',
        ),
    Tab(
        'DD°, DD.DD\'',
        'lat_dmm_d',
        'lat_dmm_m',
        'lon_dmm_d',
        'lon_dmm_m',
        template='crispy_overrides/tab-link.html',
    ),
    Tab(
        'DD°, DD\', SS.SS\'\'',
        'lat_dms_d',
        'lat_dms_m',
        'lat_dms_s',
        'lon_dms_d',
        'lon_dms_m',
        'lon_dms_s',
        template='crispy_overrides/tab-link.html',
    ),
    Tab('UTM',
        'utm_zone',
        'utmx',
        'utmy',
        template='crispy_overrides/tab-link.html'
    ),
),

tab-link.html

<li class="tab-pane{% if 'active' in link.css_class %} active{% endif %}"><a href="#{{ link.css_id }}" data-toggle="tab">{{ link.name|capfirst }}{% if tab.errors %}!{% endif %}</a></li>

【问题讨论】:

    标签: django django-crispy-forms


    【解决方案1】:

    当然,我在发布问题后马上就想通了。 叹息

    显然,对于 Tab 对象,您需要覆盖 link_template 而不是 template。看起来这可能是一个错误。

    布局类的相关部分:

    class Tab(Container):
        """
        Tab object. It wraps fields in a div whose default class is "tab-pane" and
        takes a name as first argument. Example::
            Tab('tab_name', 'form_field_1', 'form_field_2', 'form_field_3')
        """
        css_class = 'tab-pane'
        link_template = '%s/layout/tab-link.html' # Notice how it isn't 'template'
    
        def render_link(self, template_pack=TEMPLATE_PACK, **kwargs):
            """
            Render the link for the tab-pane. It must be called after render so css_class is updated
            with active if needed.
            """
            link_template = self.link_template % template_pack
            return render_to_string(link_template, {'link': self})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-10
      • 2018-07-08
      • 2021-06-23
      • 2020-03-25
      • 2023-04-07
      • 2019-03-27
      • 2020-02-18
      • 1970-01-01
      相关资源
      最近更新 更多