部署时,项目无法识别自定义过滤器模板标签(Django + uwsgi + nginx)

时间:2019-07-05 14:24:35

标签: django nginx uwsgi

将个人项目部署到ubuntu服务器(16.04)时,可以使用命令(python manage.py runserver 0.0.0.0:8000)访问网页上的项目,但不会加载静态文件。但是,使用(uwigs)命令运行时,错误被报告为无法访问。

(我不知道这是否是网络问题,无法显示上载的图像,因此我重用了代码,希望您能理解我的问题并为我提供帮助,谢谢)

项目描述:(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 :(自定义时间过滤器)

# 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

当我在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

运行uwsgi命令页面以报告错误

(django-env-py3) root@li:/srv/xfz# uwsgi --ini xfz_uwsgi.ini 

错误消息:

# http://192.168.164.128/
TemplateSyntaxError at /
'news_filters' is not a registered tag library. 

我使用了一些方法,在设置中添加了“库”,但是运行uwsgi命令后,该页面的报告不正确。

TEMPLATES = [
    {
        --snip--
            # 配置模板的static标签;
            'builtins': [
                'django.templatetags.static'
            ],
            'libraries':{
                'news_filters': 'apps.news.templatetags.news_filters',
                'payinfo_filters': 'apps.payinfo.templatetags.payinfo_filters'
            }
        },
    },
]

错误消息

# 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'

我不知道你是否能理解我的问题。如果可以,请你帮助我。谢谢。

1 个答案:

答案 0 :(得分:0)

尝试

TEMPLATES = [
    {
        --snip--
            # 配置模板的static标签;
            'builtins': [
                'django.templatetags.static'
            ],
            'libraries':{
                'news_filters': 'news.templatetags.news_filters',
                'payinfo_filters': 'payinfo.templatetags.payinfo_filters'
            }
        },
    },
]

我不确定为什么它不会自动为您解决此问题,看起来您已正确完成了所有操作