Use the django-suit template to add custom menus and custom pages, and set access permissions in the management background

1. First add the configuration in the settings.pyinside. The SUIT_CONFIGconfiguration we usually add is of apptype. If we need to customize the page, it cannot be used . It appneeds to be used url. Here we use it as follows:

# django-suit config
SUIT_CONFIG = {
    'ADMIN_NAME': 'X·X',
    'HEADER_DATE_FORMAT': '',
    'HEADER_TIME_FORMAT': 'H:i',
    'SHOW_REQUIRED_ASTERISK': True,
    'CONFIRM_UNSAVED_CHANGES': True,
    'LIST_PER_PAGE': 20,
    'MENU_OPEN_FIRST_CHILD': True,
    'MENU': (
        # sites是默认原先的app和models
        # 'sites',
        '-',
        {'app': 'auth', 'label': u'权限管理', 'icon': 'icon-lock'},
        '-',
        {'app': 'duser', 'label': u'平台用户', 'icon': 'icon-user'},
        '-',
        {'app': 'dtheme', 'label': u'主题管理', 'icon': 'icon-tags'},
        '-',
        {'app': 'dpost', 'label': u'文章管理', 'icon': 'icon-edit'},
        '-',
        # 如果使用http这种绝对路径的话,菜单不会展开,且不会标记为active状态
        {'url': '/admin/theme/mysql', 'label': u'第三数据', 'icon': 'icon-lock'},
        '-',
        {'label': u'统计数据', 'icon': 'icon-tags', 'models': (
            {'url': '/admin/theme/data', 'label': u'第一数据'},
            {'url': '/admin/theme/show', 'label': u'第二数据'}
        )}
    )
}

2. Then urls.pyadd a route inside. This route must be added in admin.site.urlsfront, because otherwise, it will go admin.site.urlsinside to match first, causing confusion or error.

from dtheme import views

urlpatterns = [
    # 第一个就是我们自己新增的url路径
    url(r'^admin/theme/data', views.data),
    url(r'^admin/', admin.site.urls),
    url(r'^api/user/', include('duser.urls')),
    url(r'^api/post/', include('dpost.urls')),
    url(r'^api/theme/', include('dtheme.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

3. Then, it is written views. If we write in the dthememodule views:

def data(request):
    return render(request, "data.html")

4. Please note that the reason why we can use it directly data.htmlis because we dthemecreated a templatesfolder under the module and put data.htmlit in it, it will scan this folder to find the template. So what does this template look like? We just wrote something at random. It should be noted here that we need to inherit base_site.html, otherwise those headerand footer, and the menu on the left will be gone, which is equivalent to no one inheriting. The content can be written in contentit.

{% extends "admin/base_site.html" %}

{% block content %}
hello, new page.
{% endblock %}

5、OVER。

6. Come back, it's not over yet. At this time, if we log out from the background and enter it directly in the browser http://127.0.0.1:8000/admin/theme/data, we find that we can still access this page directly, and it will ask you to verify if we enter other pages in the management background. So this page we customized is still very dangerous. Others can access it directly after knowing the URL. Our idea is actually very simple, and we don't want to do anything special. In terms of security, we only need to cooperate with other backgrounds If the user is already logged in, he can access it. If he does not log in and fails to pass the verification, he cannot access it and jumps directly to the login page. This requires us to viewmake settings inside.

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

def data(request):
    return render(request, "data.html")

data = staff_member_required(data)

Did you see the change above? That is, we introduced a staff_member_requiredmodule, which is used to verify whether it is an internal person (that is, whether to log in). Of course we're going to put our viewfunction inside it. That's it.

auth7. There is also a pit on how to add custom pages to it, and I will come back and add it after the research is completed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325826710&siteId=291194637