Reputation: 3
When a personal project is deployed to a ubuntu server (16.04), the command ( python manage.py runserver 0.0.0.0:8000) can be used to access the project on the web page, but the static file is not loaded. However, when running with the (uwigs) command, the error was reported to be inaccessible.
(I don't know if it is a network problem, the uploaded image can not be displayed, so I reused the code instead, I hope you can understand my problem and help me, thank you)
Project Description:(django2.1,python3)
xfz(Project structure)
apps
news
migrations
templatetags
__init__.py
news_filters.py
__init__.py
front
xfz
# settings.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
# 修改templates的路径;
'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',
],
# 配置模板的static标签;
'builtins': [
'django.templatetags.static'
],
},
},
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'front','static_dist')
STATICFILES_DIRS = [
os.path.join(BASE_DIR,'front','dist'),
]
news_filter:(Custom time filter)
# news_filters.py
# encoding: utf-8
from django import template
from datetime import datetime
from django.utils.timezone import now as now_func,localtime
register = template.Library()
@register.filter
def time_since(value):
if not isinstance(value,datetime):
return value
now = now_func()
# timedelay.total_seconds
timestamp = (now - value).total_seconds()
if timestamp < 60:
return '刚刚'
elif timestamp >= 60 and timestamp < 60*60:
minutes = int(timestamp/60)
return '%s分钟前' % minutes
elif timestamp >= 60*60 and timestamp < 60*60*24:
hours = int(timestamp/60/60)
return '%s小时前' % hours
elif timestamp >= 60*60*24 and timestamp < 60*60*24*30:
days = int(timestamp/60/60/24)
return '%s天前' % days
else:
return value.strftime("%Y/%m/%d %H:%M")
# 后台轮播图列表所引用的时间格式;
@register.filter
def time_format(value):
if not isinstance(value,datetime):
return value
# 使用localtime转化为当地时间;
return localtime(value).strftime("%Y/%m/%d %H:%M:%S")
ubuntu_server(uwsgi+nginx):
# uwsgi Configuration
[uwsgi]
# 项目的路径
chdir = /srv/xfz
# django的wsgi文件
module = xfz.wsgi
# python虚拟环境的路径
home = /root/.virtualenvs/django-env-py3
# socket文件路径
socket = /srv/xfz/xfz.sock
# 设置socket的权限
chmod-socket = 666
# 退出时是否清理环境
vacuum = true
------Dividing line------
# nginx Configuration
upstream xfz{
server unix:///srv/xfz/xfz.sock;
}
# 配置服务器
server{
# 监听的端口号
listen 80;
# 域名
server_name 192.168.164.128;
charset utf-8;
# 文件最大上传限度
client_max_body_size 75M;
# 静态文件访问的url
location /static{
# 静态文件地址
alias /srv/xfz/front/static_dist;
}
# 发送非静态文件请求到django服务器
location / {
uwsgi_pass xfz;
# uwsgi_params 文件地址
include /etc/nginx/uwsgi_params;
}
}
enter code here
When I run the command in Xshell:
# The web page can be opened normally, but the static file is not loaded.
(django-env-py3) root@li:/srv/xfz# python manage.py runserver 0.0.0.0:8000
Run the uwsgi command page to report an error
(django-env-py3) root@li:/srv/xfz# uwsgi --ini xfz_uwsgi.ini
Error messages:
# http://192.168.164.128/
TemplateSyntaxError at /
'news_filters' is not a registered tag library.
I have used some methods, add "libraries" in settings,But after running the uwsgi command, the page is reported incorrectly.
TEMPLATES = [
{
--snip--
# 配置模板的static标签;
'builtins': [
'django.templatetags.static'
],
'libraries':{
'news_filters': 'apps.news.templatetags.news_filters',
'payinfo_filters': 'apps.payinfo.templatetags.payinfo_filters'
}
},
},
]
Error message
# http://192.168.164.128/
InvalidTemplateLibrary at /
Invalid template library specified. ImportError raised when trying to load 'apps.news.templatetags.news_filters': No module named 'apps.news.templatetags'
I don't know if you can understand my problem. Please help me if you can. Thank you.
Upvotes: 0
Views: 116
Reputation: 94
Try
TEMPLATES = [
{
--snip--
# 配置模板的static标签;
'builtins': [
'django.templatetags.static'
],
'libraries':{
'news_filters': 'news.templatetags.news_filters',
'payinfo_filters': 'payinfo.templatetags.payinfo_filters'
}
},
},
]
I'm not sure why it's not automatically resolving this for you, it looks like you have done everything correctly
Upvotes: 0