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

时间:2015-05-03 19:34:52

标签: django admin class-based-views

我使用基于类的视图创建了一些表单,现在我想将它们添加到Django Admin站点。我只找到了这个link,其中介绍了向管理网站添加普通视图。

4 个答案:

答案 0 :(得分:5)

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

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

要使用此视图,您必须将其设为callable

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

一旦您的观点为callable,您就可以像处理基于函数的视图一样对待它。

要将其链接到管理网址,您可以执行以下操作:

# 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

答案 1 :(得分:1)

基于类的视图的

foldr方法返回常规视图,因此您可以从as_view()中调用它,如下所示:

ModelAdmin

答案 2 :(得分:1)

您可以直接传递基于类的视图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

答案 3 :(得分:0)

要与Django管理模板完全集成,您可以将模型管理员作为额外参数传递给CBV,然后使用它向上下文添加一些糖:

文件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(MyModelAdmin, self).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

file 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(ProcessObjectView, self).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

file process_object.html:

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

{% block content %}
...