<small id='wqkIU'></small><noframes id='wqkIU'>

    <bdo id='wqkIU'></bdo><ul id='wqkIU'></ul>

  • <i id='wqkIU'><tr id='wqkIU'><dt id='wqkIU'><q id='wqkIU'><span id='wqkIU'><b id='wqkIU'><form id='wqkIU'><ins id='wqkIU'></ins><ul id='wqkIU'></ul><sub id='wqkIU'></sub></form><legend id='wqkIU'></legend><bdo id='wqkIU'><pre id='wqkIU'><center id='wqkIU'></center></pre></bdo></b><th id='wqkIU'></th></span></q></dt></tr></i><div id='wqkIU'><tfoot id='wqkIU'></tfoot><dl id='wqkIU'><fieldset id='wqkIU'></fieldset></dl></div>
    <tfoot id='wqkIU'></tfoot>

      1. <legend id='wqkIU'><style id='wqkIU'><dir id='wqkIU'><q id='wqkIU'></q></dir></style></legend>

        Django 小部件覆盖模板

        Django widget override template(Django 小部件覆盖模板)
          <tbody id='o42ww'></tbody>

        <small id='o42ww'></small><noframes id='o42ww'>

        • <bdo id='o42ww'></bdo><ul id='o42ww'></ul>
          <legend id='o42ww'><style id='o42ww'><dir id='o42ww'><q id='o42ww'></q></dir></style></legend>
            1. <tfoot id='o42ww'></tfoot>

                1. <i id='o42ww'><tr id='o42ww'><dt id='o42ww'><q id='o42ww'><span id='o42ww'><b id='o42ww'><form id='o42ww'><ins id='o42ww'></ins><ul id='o42ww'></ul><sub id='o42ww'></sub></form><legend id='o42ww'></legend><bdo id='o42ww'><pre id='o42ww'><center id='o42ww'></center></pre></bdo></b><th id='o42ww'></th></span></q></dt></tr></i><div id='o42ww'><tfoot id='o42ww'></tfoot><dl id='o42ww'><fieldset id='o42ww'></fieldset></dl></div>
                2. 本文介绍了Django 小部件覆盖模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 django 的新手.

                  我想创建一个自定义小部件.

                  forms.py:

                  从 project.widgets 导入 MultiChoiceFilterWidget类 CustomSearchForm(FacetedSearchForm):测试颜色 = [uBlau",uRot",uGelb"]颜色 = forms.MultipleChoiceField(label=_("Color"), 选择=[(x, x) for x in TEST_COLORS],小部件=MultiChoiceFilterWidget,必需=假)

                  widget.py:

                  类 MultiChoiceFilterWidget(forms.widgets.CheckboxSelectMultiple):template_name = '项目/小部件/filter.html'option_template_name = 'ptoject/widgets/filter_option.html'

                  项目/小部件/filter.html:

                   <h1>TEST</h1>

                  但它不会呈现新的模板,而是仍然呈现旧的方式.

                  你能给我一些建议吗?

                  解决方案

                  Django 版本

                  1.11:

                  小部件必须实现 render 方法才能渲染不同的模板:

                  from django.utils.safestring import mark_safe从 django.template.loader 导入 render_to_stringMultiChoiceFilterWidget 类(forms.widgets.CheckboxSelectMultiple):template_name = '项目/小部件/filter.html'def 渲染(自我,数据):...用数据做事...return mark_safe(render_to_string(self.template_name))

                  <小时>Django 1.11 版:

                  在 renderer 的文档中,我们可以找到以下内容:

                  <块引用>

                  Django 1.11 中的新功能:

                  在旧版本中,小部件是使用 Python 呈现的.本文档中描述的所有 API 都是新的.

                  查看 widget 源代码 特别是关于 Input 小部件如何扩展 Widget 类,我们可以看到您只需要按如下方式自定义小部件:

                  类 MultiChoiceFilterWidget(forms.widgets.CheckboxSelectMultiple):template_name = '项目/小部件/filter.html'

                  这是你已经拥有的.

                  I am new at django.

                  I want to create a custom widget.

                  forms.py:

                  from project.widgets import MultiChoiceFilterWidget
                  
                  class CustomSearchForm(FacetedSearchForm):
                      TEST_COLORS = [
                          u"Blau", u"Rot", u"Gelb"
                      ]
                  
                      color = forms.MultipleChoiceField(
                          label=_("Color"), choices=[(x, x) for x in TEST_COLORS],
                          widget=MultiChoiceFilterWidget, required=False)
                  

                  widget.py:

                  class MultiChoiceFilterWidget(forms.widgets.CheckboxSelectMultiple):
                      template_name = 'project/widgets/filter.html'
                      option_template_name = 'ptoject/widgets/filter_option.html'
                  

                  project/widgets/filter.html:

                   <h1>TEST</h1>
                  

                  But it doesn't render the new template, instead it still renders the old way.

                  Can you give me some tips?

                  解决方案

                  Django version < 1.11:

                  The widget must implement the render method in order to render a different template:

                  from django.utils.safestring import mark_safe
                  from django.template.loader import render_to_string
                  
                  class MultiChoiceFilterWidget(forms.widgets.CheckboxSelectMultiple):
                      template_name = 'project/widgets/filter.html'
                  
                      def render(self, data):
                          ...
                          Do stuff with data
                          ...
                          return mark_safe(render_to_string(self.template_name))
                  


                  Django version 1.11:

                  In the renderer's documentation, we can find the following:

                  New in Django 1.11:

                  In older versions, widgets are rendered using Python. All APIs described in this document are new.

                  And by having a look at the widget source code and specifically on how the Input widget extends the Widget class, we can see that you would only need to customize your widget as follows:

                  class MultiChoiceFilterWidget(forms.widgets.CheckboxSelectMultiple):
                      template_name = 'project/widgets/filter.html'
                  

                  Which is what you have already.

                  这篇关于Django 小部件覆盖模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                  <small id='JzqZx'></small><noframes id='JzqZx'>

                      <tfoot id='JzqZx'></tfoot>
                      <i id='JzqZx'><tr id='JzqZx'><dt id='JzqZx'><q id='JzqZx'><span id='JzqZx'><b id='JzqZx'><form id='JzqZx'><ins id='JzqZx'></ins><ul id='JzqZx'></ul><sub id='JzqZx'></sub></form><legend id='JzqZx'></legend><bdo id='JzqZx'><pre id='JzqZx'><center id='JzqZx'></center></pre></bdo></b><th id='JzqZx'></th></span></q></dt></tr></i><div id='JzqZx'><tfoot id='JzqZx'></tfoot><dl id='JzqZx'><fieldset id='JzqZx'></fieldset></dl></div>
                    1. <legend id='JzqZx'><style id='JzqZx'><dir id='JzqZx'><q id='JzqZx'></q></dir></style></legend>
                            <tbody id='JzqZx'></tbody>
                            <bdo id='JzqZx'></bdo><ul id='JzqZx'></ul>