简体   繁体   English

覆盖管理员视图-Django

[英]Overriding admin views - Django

I am using Django 1.3 and python 2.7 .I am using Django admin app.What I want is when a superuser logs-in it should be shown admin/index.html with all models which is default behaviour but if any other user logs-in that is not superuser then it should be shown a totally different template with my data (like 'abc.html').What should I do to accomplish this?I guess I need to override some admin view to do this but have no idea? 我正在使用Django 1.3python 2.7 。我正在使用Django管理应用程序。我想要的是当superuser登录时,所有模式都应显示admin / index.html,这是默认行为,但如果有其他用户登录那不是超级用户,那么应该显示一个与我的数据完全不同的模板(例如“ abc.html”)。我该怎么做?我想我需要重写一些管理视图来执行此操作,但不知道吗?

Please help.If you want more information plz comment :) 请帮助。如果您想了解更多信息,请评论:)

I would create a middleware that checks if the user is a superuser or not. 我会创建一个中间件来检查用户是否是超级用户。 If the user is not supeuser you redirects him/her to the custom admin page instead of the default one. 如果用户不是超级用户,则将他/她重定向到自定义管理页面而不是默认管理页面。

class SuperUserMiddleware(object):
    def process_request(self, request):
        user = request.session.user
        if not user.is_superuser:
            return HttpResponseRedirect(NON_SUPERUSER_URL)
        ...

You have to change the view of the admin site. 您必须更改管理站点的视图。 Django Documentation mention all in detail. Django文档详细提到了所有内容。 Please check that https://docs.djangoproject.com/en/1.3/ref/contrib/admin/ if you have any error then please write back with some code details. 如果您有任何错误,请检查https://docs.djangoproject.com/en/1.3/ref/contrib/admin/ ,然后请回复一些代码详细信息。

You create a modified AdminSite class definition with additional permission rules. 您可以使用其他权限规则创建已修改的AdminSite类定义。

class SuperUserAdminSite( AdminSite ):
    def has_permission(self, request):
        return request.user.is_active and request.user.is_staff and request.user. is_superuser

Now you can create two AdminSite objects, one for ordinary users, one for super users. 现在,您可以创建两个AdminSite对象,一个用于普通用户,一个用于超级用户。

You can have two paths in your URLs for the two admin sites. 您可以在两个管理网站的网址中使用两个路径。

Superusers can use both paths. 超级用户可以使用这两种路径。

Ordinary users will only be able to use the ordinary user path in the URL. 普通用户将只能使用URL中的普通用户路径。

https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#adminsite-objects https://docs.djangoproject.com/en/1.3/ref/contrib/admin/#adminsite-objects

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM