1. First add the configuration in the settings.py
inside. The SUIT_CONFIG
configuration we usually add is of app
type. If we need to customize the page, it cannot be used . It app
needs 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.py
add a route inside. This route must be added in admin.site.urls
front, because otherwise, it will go admin.site.urls
inside 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 dtheme
module views
:
def data(request):
return render(request, "data.html")
4. Please note that the reason why we can use it directly data.html
is because we dtheme
created a templates
folder under the module and put data.html
it 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 header
and footer
, and the menu on the left will be gone, which is equivalent to no one inheriting. The content can be written in content
it.
{% 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 view
make 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_required
module, 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 view
function inside it. That's it.
auth
7. 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.