我正在尝试(在项目级别):
- 使用默认管理 View (
auth.views.login
) 作为我的登录 View - 在
ulrs.py
中设置admin.site.site_header
,以便它显示在登录页面和管理页面上
首先,这样做会更新管理页面上的site_header
...
# urls.py
admin.site.site_header = "my header name"
urlpatterns = [
url(r'^admin/?', admin.site.urls),
url(r'^', include('django.contrib.auth.urls')),
...
]
从这里看来我应该能够做到这一点:
# registration/login.html
{% extends "admin/login.html" %}
或者这个(从 django/admin/templates/login.html 复制/粘贴):
# registration/login.html
{% extends "admin/base_site.html" %}
{% load i18n static %}
{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/login.css" %}" />
{{ form.media }}
{% endblock %}
...
不要像管理 View 那样使用带有“我的标题名称”的标题。
为什么我的 registration/login.html
无法获取我在 urls.py
中设置的 admin.site.site_header
?
谢谢。
更新:
我发现默认情况下 auth/view.py
在其上下文中没有 site_header
:
# auth/view.py
def login(...
current_site = get_current_site(request)
context = {
'form': form,
redirect_field_name: redirect_to,
'site': current_site,
'site_name': current_site.name,
}
...
并且admin/sites.py
负责定义AdminSite
,它为管理站点设置site_header
class AdminSite(object):
...
def each_context(self, request):
"""
Returns a dictionary of variables to put in the template context for
*every* page in the admin site.
For sites running on a subpath, use the SCRIPT_NAME value if site_url
hasn't been customized.
"""
script_name = request.META['SCRIPT_NAME']
site_url = script_name if self.site_url == '/' and script_name else self.site_url
return {
'site_title': self.site_title,
'site_header': self.site_header,
'site_url': site_url,
'has_permission': self.has_permission(request),
'available_apps': self.get_app_list(request),
}
...
我了解了如何创建自己的 base_site.html
(硬编码 {{ "my header name }}
)以在复制/粘贴 登录中使用.html
上面,但这看起来很困惑。我想在一个地方设置这个变量并直接使用尽可能多的管理。
我的主要兴趣是基本上为普通用户帐户搭载管理应用程序。我这样做错了吗?
最佳答案
My high-level interest is to basically piggy-back the admin app for regular user accounts. Am I going about this wrong?
不幸的是,是的,你是。
https://docs.djangoproject.com/en/1.10/intro/tutorial02/
The admin isn’t intended to be used by site visitors. It’s for site managers.
使用许多经过尝试和测试的注册/身份验证包(例如 django-allauth)之一来添加身份验证系统非常容易,尝试将 django admin 重新用作用户级应用程序是浪费而且也不是很安全.
关于python - Django 1.10 : Setting admin. site.site_header 并使用默认的管理员登录 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41026487/