【问题标题】:Django admin actions in detail view?详细视图中的 Django 管理操作?
【发布时间】:2015-11-17 17:31:20
【问题描述】:

我的(相当标准的)Django 应用程序中定义了一些管理操作。如果这些操作在对象的详细信息页面上可用,其中一些操作也会很有意义。

现在,用户需要导航回列表,然后搜索特定记录,然后触发操作。

有没有办法在详细信息页面上也公开此功能?

【问题讨论】:

  • 请选择正确答案或自己提供一个

标签: python django django-admin


【解决方案1】:

Here 是您正在寻找的答案。

基本上,您创建了一个 yourmodel_changeform.html 文件,其中包含以下内容:

{% extends 'admin/change_form.html' %}

{% block submit_buttons_bottom %}
    {{ block.super }}
    <div class="submit-row">
        <input type="submit" value="Button Label" name="_your-action-name">
    </div>
{% endblock %}

然后在你的 ModelAdmin 类上覆盖 response_change 方法并设置 change_form_template 属性。

from django.http import HttpResponseRedirect

class YourModelAdmin(admin.ModelAdmin):

    change_form_template = "templatelocation/yourmodel_changeform.html"

    def response_change(self, request, obj):
        if "_your-action-name" in request.POST:
            # do whatever you want the button to do
            obj.name = "new name"
            obj.save()
            return HttpResponseRedirect(".")  # stay on the same detail page
        return super().response_change(request, obj)

在 Django 3.0.3 上测试

【讨论】:

    【解决方案2】:

    如果我理解正确,您需要做的是编写一些单独的 html,然后使用 {% include '/path/foo.html' %} 在不同的页面上使用它。 Here 更多关于这个主题。

    foo.html 中,您可以添加您想在不同页面上使用的任何功能(表单、链接等)。

    如果您想要更详细的答案,很高兴看到您的代码以及您想要做什么。

    【讨论】:

      【解决方案3】:

      详细说明pythad。

      1 添加另一个简单的 html 来扩展您的管理 html 并保存描述管理操作的按钮。你可以称它为change_form.py 确保 strt 与:

      {% extends "admin/change_form.html" %}
      

      2 您需要在 admin.py 中添加小功能,以将按钮连接到操作功能。 喜欢:

              try:
                  sr = Scenario.objects.filter(pk = pk)
                  queitem = QueueScript.objects.filter(scriptresult__pk=sr.scriptreport.pk)
              #do somtho
      
              return HttpResponseRedirect(request.META["HTTP_REFERER"])
      

      3 您需要详细说明/覆盖管理员的 get_urls 函数以了解上述函数:

      def get_urls(self, ):
          urls = super(ScenarioAdmin, self).get_urls()
          my_urls = patterns("",
              url(r"(?P<pk>\d+)/stop_my_script/$",
                                 self.stop_my_script),
              url(r"(?P<pk>\d+)/run_scenario_standalone/$",
                                 self.run_scenario_standalone),
                             )
      
          return my_urls + urls
      

      4 最后在 admin.py 中详细说明你的 change_view 函数

        def change_view(self, request, object_id, form_url='', extra_context=None):
              extra_context = {}
              extra_context['show_scer_buttons'] = True
              extra_context['is_running'] = self.choose_template_buttons(object_id, extra_context)
              return super(ScenarioAdmin, self).change_view(request, object_id,
                  form_url, extra_context=extra_context)
      

      【讨论】:

        猜你喜欢
        • 2020-10-30
        • 2017-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-23
        • 2013-10-22
        • 2020-08-31
        相关资源
        最近更新 更多