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

        <legend id='IyGcJ'><style id='IyGcJ'><dir id='IyGcJ'><q id='IyGcJ'></q></dir></style></legend>
        <tfoot id='IyGcJ'></tfoot>
        • <bdo id='IyGcJ'></bdo><ul id='IyGcJ'></ul>

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

      1. 在 Django 的 admin change_form 中创建自定义按钮

        时间:2023-11-07
          <tbody id='7aOpH'></tbody>
      2. <small id='7aOpH'></small><noframes id='7aOpH'>

        • <legend id='7aOpH'><style id='7aOpH'><dir id='7aOpH'><q id='7aOpH'></q></dir></style></legend>

            <tfoot id='7aOpH'></tfoot>
                <bdo id='7aOpH'></bdo><ul id='7aOpH'></ul>
                1. <i id='7aOpH'><tr id='7aOpH'><dt id='7aOpH'><q id='7aOpH'><span id='7aOpH'><b id='7aOpH'><form id='7aOpH'><ins id='7aOpH'></ins><ul id='7aOpH'></ul><sub id='7aOpH'></sub></form><legend id='7aOpH'></legend><bdo id='7aOpH'><pre id='7aOpH'><center id='7aOpH'></center></pre></bdo></b><th id='7aOpH'></th></span></q></dt></tr></i><div id='7aOpH'><tfoot id='7aOpH'></tfoot><dl id='7aOpH'><fieldset id='7aOpH'></fieldset></dl></div>
                  本文介绍了在 Django 的 admin change_form 中创建自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想在管理界面的添加/更改表单中添加自定义按钮.默认只有三个:

                  I want to add custom buttons to the add/change form at the administration interface. By default, there are only three:

                  • 保存并添加另一个

                  • Save and add another

                  保存并继续编辑

                  保存

                  我在 forms.py 文件中创建了一些自定义方法,我想创建按钮来调用这些方法.我使用了片段 http://djangosnippets.org/snippets/1842/,但并不完全是我想要的是.这个允许从 admin.py 文件而不是 forms.py.

                  I have created some custom methods in my forms.py file, and I want to create buttons to call these methods. I have used the snippet http://djangosnippets.org/snippets/1842/, but it's not exactly what I want. This one allows to create buttons and call methods from the admin.py file and not forms.py.

                  有没有办法做到这一点?

                  Is there a way to do that?

                  这是我的 admin.py 代码:

                  class CategoryAdmin(admin.ModelAdmin):
                      prepopulated_fields = { "alias": ("title",) }
                      form = CategoryForm
                  
                  admin.site.register(Category, CategoryAdmin)
                  

                  还有我的 forms.py 代码,

                  class CategoryForm(forms.ModelForm):
                      """
                      My attributes
                      """
                      def custom_method(self):
                          print("Hello, World!")
                  

                  如何创建一个调用custom_method()"的按钮?

                  How do I create a button that calls "custom_method()"?

                  推荐答案

                  您可以覆盖 admin/change_form.html.将 contrib.admin.templates 中的版本复制到您的项目中.我的是 myproject/templates/admin/change_form.html,但你可以使用 /myproject/myapp/templates/admin/change_form.html.

                  You can override admin/change_form.html. Copy the version in contrib.admin.templates into your project. Mine is myproject/templates/admin/change_form.html, but you could use /myproject/myapp/templates/admin/change_form.html.

                  接下来,编辑副本并将对现有模板标签 {% submit_row %} 的两个引用更改为指向您自己的模板标签 {% my_template_tag %}.

                  Next, edit the copy and change the two references to the existing template tag, {% submit_row %}, to point to your own template tag, {% my_template_tag %}.

                  将您的模板标签基于 contrib.admin{% submit_row %},但编辑 HTML 模板以包含您想要显示的任何额外按钮.

                  Base your template tag on the contrib.admin's {% submit_row %}, but edit the HTML template to contain any extra buttons you want to display.

                  这篇关于在 Django 的 admin change_form 中创建自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

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