Django - custom admin page not related to a model

北慕城南 提交于 2019-12-05 02:55:57

问题


I am using Django 1.7 with Mezzanine. I would like to have some page in admin, where the staff can call some actions (management commands etc.) with buttons and other control elements.

I would also like to avoid creating new model, or manually create a template and add link to it (if possible).

What is the most common/clean ways how to achieve that?


回答1:


Actually it is simpler. Just before urlpatterns in urls.py patch admin urls like that:

def get_admin_urls(urls):
    def get_urls():
        my_urls =  patterns('',
           url(r'^$', YourCustomView,name='home'), 
        )
        return my_urls + urls
    return get_urls

admin.autodiscover()

admin_urls = get_admin_urls(admin.site.get_urls())
admin.site.get_urls = admin_urls



回答2:


ModelAdmin.get_urls let you add a url to the admin url's. So you can add your own view like this:

class MyModelAdmin(admin.ModelAdmin):
    def get_urls(self):
        urls = super(MyModelAdmin, self).get_urls()
        my_urls = patterns('',
            (r'^my_view/$', self.my_view)
        )
        return my_urls + urls

    def my_view(self, request):
        # custom view which should return an HttpResponse
        pass

https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_urls

I didn't try this out, but it seems to me that you can subclass a build-in admin view and let your custom template extend the build-in admin templates.



来源:https://stackoverflow.com/questions/26325506/django-custom-admin-page-not-related-to-a-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!