在使用uwsgi + nginx + django部署项目的时候,如果使用到自己定义的过滤器,很容易会出现500错误(如标题)
解决办法:
- 使用python manage.py runserver确保自己的过滤器可以使用
- 在settings.py中的TEMPLATES中添加 ‘libraries’ 配置信息
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'front', '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',
],
'builtins': ['django.templatetags.static'],
'libraries':{
'filter_name': 'your_apps.templatetags.your_filter'
},
},
},
]
- 添加**‘libraries’**后可能会出现 No Module Name xxx 错误,这时候需要在INSTALL_APPS添加自定义的过滤器
INSTALLED_APPS = [
...
'your_apps.templatetags.your_filter',
...
]