django admin,使用自定义视图扩展管理员

时间:2016-03-08 18:44:24

标签: python django

我想就此事请求一些帮助。

我已按照this指南向我的管理员添加了一个视图。

我使用的是与网站相同的代码,我收到了错误消息:

Reverse for 'app_list' with arguments '()' and keyword arguments '{'app_label': 'reports'}' not found.

admin/my_view工作正常,但如果我尝试访问管理员中的其他页面,例如索引页面,我收到错误

以下是代码:

#someapp/admin.py

from django.conf.urls import patterns
from django.contrib import admin
from django.http import HttpResponse

def my_view(request):
    return HttpResponse("Hello!")

def get_admin_urls(urls):
    def get_urls():
        my_urls = patterns('',
            (r'^my_view/$', admin.site.admin_view(my_view))
         )
        return my_urls + urls
    return get_urls

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

我正在使用django 1.8.2,python 2.7

另外,在管理员索引页面中添加一些视图或上下文的最佳方法是什么?

更新

我尝试过Antoine Pinsard的答案并尝试了这个:

#admin.py
from django.contrib.admin import AdminSite
class MyAdminSite(AdminSite):

    def get_urls(self):
        from django.conf.urls import url
        urls = super(MyAdminSite, self).get_urls()
        urls += [
            url(r'^my_view/$', self.admin_view(self.my_view))
        ]
        return urls

    def my_view(self, request):
        return HttpResponse("Hello!")

admin_site = MyAdminSite()

将urls.py的url(r'^admin/', include(admin.site.urls)),替换为url(r'^admin/', include(admin_site.urls)),

得到:

Reverse for 'app_list' with arguments '()' and keyword arguments

TRACEBACK:

Request Method: GET
Request URL: http://localhost:8000/@dmin/

Django Version: 1.8.2
Python Version: 2.7.10
Installed Applications:
('grappelli',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.sessions.models',
 'frontend',
 'file_maintenance',
 'reports',
 'transactions',
 'admin_reorder',
 'admin_notifications',
 'django_twilio',
 'daterange_filter')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.models.User',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware',
 'django.middleware.security.SecurityMiddleware',
 'admin_reorder.middleware.ModelAdminReorder')


Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  157.                     response = middleware_method(request, response)
File "C:\Python27\lib\site-packages\admin_reorder\middleware.py" in process_template_response
  134.         self.init_config(request, app_list)
File "C:\Python27\lib\site-packages\admin_reorder\middleware.py" in init_config
  21.         admin_index = admin.site.index(request)
File "C:\Python27\lib\site-packages\django\views\decorators\cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "C:\Python27\lib\site-packages\django\contrib\admin\sites.py" in index
  438.                                 current_app=self.name,
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in reverse
  579.     return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in _reverse_with_prefix
  496.                              (lookup_view_s, args, kwargs, len(patterns), patterns))

Exception Type: NoReverseMatch at /@dmin/
Exception Value: Reverse for 'app_list' with arguments '()' and keyword arguments '{'app_label': 'django_twilio'}' not found. 0 pattern(s) tried: []

4 个答案:

答案 0 :(得分:4)

您链接的指南很旧,我很惊讶在去年左右没有找到任何直接回答您问题的内容。

  1. 在您应用的 admin.py 中或在方便的位置创建新的Admin Site
  2. 在新的 AdminSite 中创建一个功能,通过您的额外网址扩充get_urls()功能。
  3. 确保您的项目 urls.py 指向新创建的 AdminSite
  4. 以下适用于Python 3.5.1和Django 1.9.6。

    <强>程序my_app / admin.py

    from django.contrib import admin
    from django.contrib.admin import AdminSite
    from django.http import HttpResponse
    
    from my_app.models import SomeModel
    
    
    class MyAdminSite(AdminSite):
    
        def custom_view(self, request):
            return HttpResponse("Test")
    
        def get_urls(self):
            from django.conf.urls import url
            urls = super(MyAdminSite, self).get_urls()
            urls += [
                url(r'^custom_view/$', self.admin_view(self.custom_view))
            ]
            return urls
    
    admin_site = MyAdminSite()
    
    
    @admin.register(SomeModel, site=admin_site)
    class SomeModelAdmin(admin.ModelAdmin):
        pass
    

    <强> MY_PROJECT / urls.py

    from django.conf.urls import url, include
    
    from my_app.admin import admin_site
    
    urlpatterns = [
        url(r'^admin/', admin_site.urls),
        ...
    ]
    

答案 1 :(得分:1)

对于Django 1.4+,以下是解决方案:

from django.conf.urls import url
from django.contrib import admin
from .models import MyModel


class MyAdmin(admin.ModelAdmin):
    list_display = (...)

    def custom_admin_view(self, request):
        # your logic here
        if request.method == 'POST':
            ...
        else:
            ...
        return HttpResponse(...)

    def get_urls(self):
        additional_urls = [
            url(r'^custom/$', self.admin_site.admin_view(self.custom_admin_view), name='custom')
        ]
        # append your custom URL BEFORE default ones
        return additional_urls + super().get_urls()


admin.site.register(MyModel, MyAdmin)

在所有其他之前添加您的自定义URL很重要。否则,您可能会重定向到管理员change_view

答案 2 :(得分:0)

对于Django 2 +:

from django.http import HttpResponse
from django.contrib.admin.views.decorators import staff_member_required

@staff_member_required
def my_custom_auth_url(request):
    return HttpResponse("Authorized URL")

staff_member_required装饰器检查是否在admin用户模型中同时检查了is_active和is_staff。然后,只需更新URLs.py以创建指向此功能的端点即可。可能还有其他装饰器检查超级用户状态等,但这解决了我的用例。

实现上述功能后,我发现了以下装饰器

from django.contrib.auth.decorators import login_required

@login_required
def do_stuff(request):
    pass

如果用户未登录,它将重定向到:“ settings.LOGIN_URL”

答案 3 :(得分:0)

下面的代码将override默认的管理站点写入您的自定义视图。

适用于Django的作品> = 2.1:

### myproject / admin.py

Get the list of all firewall rules;
For each of the firewall rules {
  Get the networkTags for that rule;
  Search for all compute instances that have one or more of those tags;
  List the rule and the associated compute instances that have the tags;
}

### myproject / apps.py

from django.contrib import admin
from django.http import HttpResponse

class MyAdminSite(admin.AdminSite):

    def my_view(self, request): # your custom view function
        return HttpResponse("Test")

    def get_urls(self):
        from django.urls import path

        urlpatterns = super().get_urls()
        urlpatterns += [
            path('my_view/', self.admin_view(self.my_view))
        ]
        return urlpatterns

### myproject / settings.py

from django.contrib.admin.apps import AdminConfig

class MyAdminConfig(AdminConfig):
    default_site = 'myproject.admin.MyAdminSite'

然后转到http://127.0.0.1:8000/admin/my_view/