【问题标题】:How do you remove the save and continue editing button in django-admin in django 1.9 so that it is not present?如何在 django 1.9 中删除 django-admin 中的保存并继续编辑按钮,使其不存在?
【发布时间】:2016-12-21 05:46:12
【问题描述】:

我已经尝试了In Django admin, how can I hide Save and Continue and Save and Add Another buttons on a model admin? 中列出的所有解决方案,而那篇帖子是几年前的,我找不到任何关于如何禁用这些按钮的信息。由于自定义保存,保存并继续编辑按钮会导致错误。有没有办法在当前版本的 django 中禁用它? 约束: - 不能将应用程序放在 django.contrib.admin 之前 - 我只需要为一种形式禁用它。 - 我有一个用于创建的自定义表单,它有一个自定义保存方法(这是一个帐户创建)

【问题讨论】:

    标签: django django-admin django-1.9


    【解决方案1】:

    您可以只隐藏按钮(底层功能仍然存在,但按钮将不可见)。

    这应该可以工作

    from django.contrib import admin
    
    class MyModelAdmin(admin.ModelAdmin):
        ...
    
        class Media:
            css = {
                'all': ('some/path/to/css/disable_save_and_continue_editing_button.css')
            }
    

    disable_save_and_continue_editing_button.css

    input[name="_continue"] {
      display: none;
    }
    

    【讨论】:

      【解决方案2】:

      所以我想通了。如果您需要使用这些按钮,请复制submit_line.html 的代码,然后将其覆盖到您的templates/admin/submit_line.html 并转到您的setting.py,然后转到

         TEMPLATES = [
          {
              'BACKEND': 'django.template.backends.django.DjangoTemplates',
              'DIRS': [os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))),"Project name or app name depends where you put your templates folder","templates")],
              'APP_DIRS': True,
              'OPTIONS': {
                  'context_processors': [
                      'django.template.context_processors.debug',
                      'django.template.context_processors.request',
                      'django.contrib.auth.context_processors.auth',
                      'django.contrib.messages.context_processors.messages',
                  ],
              },
          },
      ]
      

      在你的submit_line.html 代码中

          {% load i18n admin_urls %}
      <div class="submit-row">
      {% if show_save %}<input type="submit" value="{% trans 'Save' %}" class="default" name="_save">{% endif %}
      {% if show_delete_link %}<p class="deletelink-box"><a href="{% url opts|admin_urlname:'delete' original.pk|admin_urlquote %}"     class="deletelink">{% trans "Delete" %}</a></p>{% endif %}
      {% if show_save_as_new %}<input type="submit" value="{% trans 'Save as new' %}" name="_saveasnew" {{ onclick_attrib }}/>{%endif%}
      {% if show_save_and_add_another %}<input type="submit" value="{% trans 'Save and add another' %}" name="_addanother" {{ onclick_attrib }}/>{% endif %}
      {% if show_save_and_continue %}<input type="submit" value="{% trans 'Save and continue editing' %}" name="_continue" {{ onclick_attrib }}/>{% endif %}
      </div>
      

      只需删除 savecontinue 按钮,或者您可以简单地评论它。 希望这会有所帮助。 如果有帮助,请将其标记为正确。

      【讨论】:

        猜你喜欢
        • 2017-12-17
        • 2012-01-16
        • 2018-04-26
        • 1970-01-01
        • 1970-01-01
        • 2012-03-21
        • 1970-01-01
        • 2014-04-18
        • 1970-01-01
        相关资源
        最近更新 更多