将基于 django class 的视图添加到管理站点

Add django class based view to admin site

我已经使用基于 class 的视图创建了一些表单,现在我想将它们添加到 Django 管理站点。我只找到了这个 link,它描述了向管理站点添加普通视图。

as_view() 基于 class 的视图的方法 returns 常规视图,因此您可以从 ModelAdmin 调用它,如下所示:

def review(self, request, id):
    return MyReviewView.as_view()(request, id)

假设您有以下基于 class 的视图:

# File: views.py
class MyAwesomeBookView(TemplateView):
    pass

要使用此视图,您必须创建它 callable

# File: views.py
awesome_book_view = MyAwesomeBookView.as_view()

一旦您的视图是 callable,您就可以像对待 基于函数的视图 一样对待它。

要link它到管理员网址,你可以这样做:

# File: admin.py
@admin.register(Book)
class BookModelAdmin(admin.ModelAdmin):

    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            url(r'^awesome-books/$', 
                self.admin_site.admin_view(awesome_book_view)),
        ]
        return my_urls + urls

您可以直接传递基于 class 的视图的 as_view,而不需要像 awesome_book_view 这样的变量,正如另一个答案所建议的那样:

示例:

admin.py 我有:

class EmailAdmin(admin.ModelAdmin):

    def get_urls(self):
        urls = super(EmailAdmin, self).get_urls()
        my_urls = [
            url(r'^send_email/$',
                self.admin_site.admin_view(SendEmailAdminView.as_view())),
        ]
        return my_urls + urls

admin.site.register(Email, EmailAdmin)

views.py 我有:

class SendEmailAdminView(View):

    def get(self, request):
        pass

    def post(self, request):
        pass

为简洁起见,我删除了大部分代码,只保留了相关部分。你可以看到完整的 code here.

为了与 Django 管理模板完全集成,您可以将模型管理作为额外参数传递给基于 class 的视图,然后使用它向上下文添加一些糖分:

文件admin.py:

from django.contrib import admin
from .models import MyModel
from .views import ProcessObjectView

@admin.register(MyModel)
class MyModelAdmin(admin.ModelAdmin):

    def get_urls(self):
        info = self.model._meta.app_label, self.model._meta.model_name
        urls = super().get_urls()
        my_urls = patterns('',
            url(r'^(?P<object_id>.*)/process/$',
                self.admin_site.admin_view(ProcessObjectView.as_view()),
                {'model_admin': self, },
                name="%s_%s_process" % info),
        )
        return my_urls + urls

文件views.py

from django.contrib.auth import get_permission_codename

class ProcessObjectView(UpdateView):

    model = MyModel
    pk_url_kwarg = "object_id"
    fields = [... ]
    template_name = 'admin/backend/mymodel/process_object.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        # see http://www.slideshare.net/lincolnloop/customizing-the-django-admin
        model_admin = self.kwargs['model_admin']
        opts = model_admin.model._meta
        admin_site = model_admin.admin_site
        has_perm = self.request.user.has_perm(opts.app_label + '.' + get_permission_codename('change', opts))
        context.update({
            'admin_site': admin_site.name,
            'title': 'Process: ' + str(self.get_object()),
            'opts': opts,
            'app_label': opts.app_label,
            'has_chage_permission': has_perm,
        })

        return context

文件process_object.html:

{% extends "admin/change_form.html" %}
{% load i18n utils_tags %}

{% block content %}
...