如何修复django管理员“您无权查看或编辑任何内容。”? [英] how to fix django admin "You don't have permission to view or edit anything."?
问题描述
为了自定义默认的管理站点,我进行了以下更改:
for the sake of customizing the default admin site , i have made the following changes:
admin.py:
class CustomAdminSite(admin.AdminSite):
def get_urls(self):
urls = super(CustomAdminSite, self).get_urls()
custom_urls = [
# re_path(r'^.*\.html', views.custom_admin_template_loader, name='custom-loader'),
url(r'^.*\.html', self.admin_view(self.my_view), name="my-view"),
]
return urls + custom_urls
def my_view(self, request):
context = {}
load_template = request.path.split('/')[-1]
template = loader.get_template('admin/' + load_template)
request.current_app = self.name
return HttpResponse(template.render(context, request))
apps.py:
from django.contrib.admin.apps import AdminConfig
class MyAdminConfig(AdminConfig):
default_site = 'myproject.admin.CustomAdminSite'
settings.py:
INSTALLED_APPS = [
'myproject.apps.MyAdminConfig',
urls.py中没有任何更改
因此,如果我访问管理员/可以,但是如果我使用自己创建的视图访问另一个模板,它说您无权编辑任何内容,如何解决该问题?
来说明为什么要使用该视图,这是因为我覆盖了默认的管理模板,并且现在有了导航栏,这意味着我需要浏览不同的html文件(模板)
so if i access admin/ it works , but if i access another template using the view i made , it says You don't have permission to edit anything , how to fix it? for clarifications on WHY i m using that view, it is because i overrided the default admin templates and now i have a navigation bar , which means i need to navigate through different html files (templates)
推荐答案
解决方案:
我需要传递上下文+设置request.current_app(在我的自定义视图中添加 )
i needed to pass context + set the request.current_app , (added in my custom view)
context = {
**self.each_context(request),
'title': self.index_title,
'app_list': app_list,
}
request.current_app = self.name
这篇关于如何修复django管理员“您无权查看或编辑任何内容。”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!