base_jinja2.py中代码:
from jinja2 import Environment
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
def environment(**options):
env = Environment(**options)
env.globals.update({
‘static’:staticfiles_storage.url,
‘url’:reverse
})
return env
settings.py中代码:
TEMPLATES = [
{
‘BACKEND’: ‘django.template.backends.jinja2.jinja2’,
‘DIRS’: [os.path.join(BASE_DIR,‘templates’)],
‘APP_DIRS’: True,
‘OPTIONS’: {
‘context_processors’: [
‘django.template.context_processors.debug’,
‘django.template.context_processors.request’,
‘django.contrib.auth.context_processors.auth’,
‘django.contrib.messages.context_processors.messages’,
],
‘environment’:‘app.base_jinja2.environment’
},
urls.py中代码:
from django.contrib import admin
from django.urls import path
from app.views import test
urlpatterns = [
path(‘admin/’, admin.site.urls),
path(‘test/’,test)