Fixing the Issue of Django Admin Being Inaccessible After Using the Jinja2 Template Engine
Two files are needed Note: These two files can be placed anywhere, but remember the path, as you'll need to enter it later during configuration. It's recommended to place them in the same directory as settings.py. Create the backends.py file: import sys from django.template.backends import jinja2 as jinja2backend from django.template.backends.utils impor
目录2 个章节
Two files are needed
Note: These two files can be placed anywhere, but remember the path, as you'll need to enter it later during configuration. It's recommended to place them in the same directory as
settings.py.
Create the backends.py file:
import sys
from django.template.backends import jinja2 as jinja2backend
from django.template.backends.utils import csrf_input_lazy, csrf_token_lazy
from django.template import TemplateDoesNotExist, TemplateSyntaxError
from django.utils.module_loading import import_string
import jinja2
import six
class Jinja2Backend(jinja2backend.Jinja2):
def __init__(self, params):
self.context_processors = [
import_string(p)
for p in params['OPTIONS'].pop('context_processors', [])
]
super(Jinja2Backend, self).__init__(params)
def from_string(self, template_code):
return Template(
self.env.from_string(template_code), self.context_processors)
def get_template(self, template_name):
try:
return Template(
self.env.get_template(template_name), self.context_processors)
except jinja2.TemplateNotFound as exc:
six.reraise(TemplateDoesNotExist, TemplateDoesNotExist(exc.args),
sys.exc_info()[2])
except jinja2.TemplateSyntaxError as exc:
six.reraise(TemplateSyntaxError, TemplateSyntaxError(exc.args),
sys.exc_info()[2])
class Template(jinja2backend.Template):
def __init__(self, template, context_processors):
self.template = template
self.context_processors = context_processors
def render(self, context=None, request=None):
if context is None:
context = {}
if request is not None:
context['request'] = request
lazy_csrf_input = csrf_input_lazy(request)
context['csrf'] = lambda: lazy_csrf_input
context['csrf_input'] = lazy_csrf_input
context['csrf_token'] = csrf_token_lazy(request)
for cp in self.context_processors:
context.update(cp(request))
# print(context)
return self.template.render(context)
The env.py file
from __future__ import absolute_import
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
from jinja2 import Environment
def environment(**options):
env = Environment(**options)
env.globals.update({
'static': staticfiles_storage.url,
'url': reverse,
})
return env
Project configuration
Edit settings.py
CONTEXT_PROCESSORS = [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
]
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {'context_processors': CONTEXT_PROCESSORS, },
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2'
,
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': CONTEXT_PROCESSORS,
'environment': '存放路径.env.environment'
},
},
]

_EOF_
Fixing the Issue of Django Admin Being Inaccessible After Using the Jinja2 Template Engine
- 本文作者:
- 程序设计实验室
- 版权声明:
- 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
相关文章
Django与Vue.js模板语法冲突问题的解决
问题 django模板与vue.js的变量都是使用 {{ 和 }} 包裹起来的,在渲染django模板时会先替代掉所有的 {{ 和 }}及被包裹在其中的内容,使得vue.js没办法用 {{ 和 }}来绑定变量。 解决方法 方法1:修改vue.js的默认的绑定符号 在Vue App创建时配置 let app = new Vue({ delimiters: [[[, ]]], el: '#vu
2022-08-26 · 0_Django开发常见问题 · 约 1 分钟
CSRF-verification-failed-Request-aborted
About the CSRF_Token (From Django Official Document) The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries. This type of attack occurs when a malici
2022-04-02 · 0_Django开发常见问题 · 约 1 分钟
StarBlog博客开发笔记:前端开发(3)之SASS与SCSS
前言 本项目需要使用SCSS来编写页面样式。 这里的SASS,是指名为SASS的css预处理器 然后SCSS是一种 CSS-like 语法 对于初学者,SCSS 是完全和 CSS 兼容的,这意味着几乎为零的学习曲线。SCSS语法即是:它只是加了一些功能的 CSS。当你和没经验的开发者一起工作时这很重要:他们可以很快开始编码而不需要首先去学习Sass。 此外,SCSS 还是 易于阅读 的,因为它是有
2024-12-13 · StarBlog前端开发-Vue · 约 1 分钟
youtube-dl下载太慢了,我选yt-dlp
前言 最近过年嘛,过年前照例来下载一些贺岁歌曲,现在国内没啥人做贺岁专辑,这方面还得看马来西亚华人,他们每年都有出专辑,质量很不错! 国内平台自然是没有(或者不全的),需要在YouTube下载~ 之前我都是用Chrome插件下载完再使用脚本合并视频,有点繁琐,今年试试自动下载的黑科技~ 作为对比的这俩工具,都用 pip 安装即可 pip install youtube-dl pip install
2023-01-24 · 杂七杂八 · 约 1 分钟
讨论
共 0 条加载评论中…