运行环境
本地:win10 企业版 版本号1809(64位)
虚拟机:VMware Workstation Pro 15.0.2 build-10952284
虚拟机系统:Ubuntu 18.04.2 LTS
python版本:3.7.2(本地+虚拟机)
Django版本: 2.1.7
uWSGI版本:2.0.18(64位)
问题:django项目在本地以python manage.py runserver的形式运行时功能正常,但在以uWSGI+nginx形式部署到Ubuntu,浏览器访问url,视图渲染过滤器的html时报错,详细报错情况如下:
TemplateSyntaxError at /'news_filters' is not a registered tag library. Must be one of:
admin_list admin_modify admin_static admin_urlscachehighlighti 18nl 10n log more_like_this payinfo_filters rest_framework static staticfiles tz
即在渲染template模板时,我的app下面的templatetags文件夹(已包含init文件)中的news_filters过滤器没有正常加载,提示过滤器没有注册,上文列出的是django环境自带的filter或者tag,在网上搜索之后,有人说要在settings.py文件中的TEMPLATES列表中添加libraries,即下文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': {
'news_filters': 'app.news.templatetags.news_filters',
},
},
},
]
我按照news_filters.py文件的路径进行添加之后依然报错,信息如下:
InvalidTemplateLibrary at /
Invalid template library specified. ImportError raised when trying to load 'app.news.templatetags.news_filters': No module named 'app.news.templatetags'
提示没有templatetags这个app,templatetags确实不是一个app,app.news才是一个app,因此我只加载了app.news(见下文),于是我将 'news_filters': 'app.news.templatetags.news_filters'改为'news_filters': 'app.news',重新运行依然报错:
Module app.news does not have a variable named 'register'
提示app没有'register'变量,我考虑'register'应该在news_filters.py中,于是我将app.news.templatetags.news_filters作为一个app添加进settings.py的INSTALLED_APPS列表中:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app.cms',
'app.xfzauth',
'app.news',
'app.news.templatetags.news_filters',
'app.course',
'app.ueditor',
'app.payinfo',
'rest_framework',
'debug_toolbar',
'haystack',
]
并将libraries中的'news_filters': 'app.news.'重新改为'news_filters': 'app.news.templatetags.news_filters',再次运行没有报错,虽然问题得到了临时性的解决,但随之又有新的问题出现,对在另一个template模板中的payinfo_filters过滤器我采取了相同的操作,但依然报错,而且是服务器的内部错误:
Traceback (most recent call last):
File "./xfz/wsgi.py", line 16, in <module>
application = get_wsgi_application()
File "/root/HOME/.virtualenvs/django/lib/python3.6/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
django.setup(set_prefix=False)
File "/root/HOME/.virtualenvs/django/lib/python3.6/site-packages/django/init.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/root/HOME/.virtualenvs/django/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate
app_config = AppConfig.create(entry)
File "/root/HOME/.virtualenvs/django/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
module = import_module(entry)
File "/root/HOME/.virtualenvs/django/lib/python3.6/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "./app/payinfo/templatetags/payinfo_filters.py", line 10, in <module>
from app.payinfo.models import PayinfoOrder
File "./app/payinfo/models.py", line 13, in <module>
class Payinfo(models.Model):
File "/root/HOME/.virtualenvs/django/lib/python3.6/site-packages/django/db/models/base.py", line 87, in new
app_config = apps.get_containing_app_config(module)
File "/root/HOME/.virtualenvs/django/lib/python3.6/site-packages/django/apps/registry.py", line 249, in get_containing_app_config
self.check_apps_ready()
File "/root/HOME/.virtualenvs/django/lib/python3.6/site-packages/django/apps/registry.py", line 132, in check_apps_ready
raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
提示没有app被加载,这样的话肯定是无法正常运行的,从报错信息的加粗部分可以看到,问题出在我在payinfo_filters.py中导入了自定义app数据库模型,即:
from app.payinfo.models import PayinfoOrder
我思考只要在过滤器(.py文件)中导入了自定义app中的任意模块或函数,按照前文提到的方法的话应该都会报错,所以通过添加过滤器的python文件到INSTALL_APPS列表的方法并不能很好的解决问题。但是显然因为我在本地运行是良好的,问题应该出在uWSGI或者ubuntu系统上,重新装虚拟机比较麻烦,而uWSGI出问题的可能性更大,重新查看最初的详细的报错信息:
Request Method: GET
Request URL: http://192.168.75.200:8000/
Django Version: 2.1.7
Exception Type: TemplateSyntaxError
Exception Value:
TemplateSyntaxError at /'news_filters' is not a registered tag library. Must be one of:
admin_list admin_modify admin_static admin_urlscachehighlighti 18nl 10n log more_like_this payinfo_filters rest_framework static staticfiles tz
Exception Location: /root/HOME/.virtualenvs/django/lib/python3.7/site-packages/django/template/defaulttags.py in find_library, line 1024
Python Executable: /usr/local/bin/uwsgi
Python Version: 3.7.2
从加粗的地方可以看到,defaulttags.py in find_library函数报错,defaulttags.py文件路径在python虚拟环境下,find_library函数代码如下:
def find_library(parser, name):
try:
return parser.libraries[name]
except KeyError:
raise TemplateSyntaxError(
"'%s' is not a registered tag library. Must be one of:\n%s" % (
name, "\n".join(sorted(parser.libraries)),
),
)
报错语句为return parser.libraries[name],即没有在parse类实例的libraries[ ]列表中找到自定义的news_filters模块(或者叫库,也就是.py文件),我么再来看一下parse类实例的初始化代码(Parse类位于与defaulttags.py路径相同的base.py文件中):
class Parser:
def __init__(self, tokens, libraries=None, builtins=None, origin=None):
self.tokens = tokens
self.tags = {}
self.filters = {}
self.command_stack = []
if libraries is None:
libraries = {}
if builtins is None:
builtins = []
self.libraries = libraries
for builtin in builtins:
self.add_library(builtin)
self.origin = origin
很明显这里并没有对libraries列表进行赋值,我们查找一下对Parse类的引用,只有在同文件的Template类下的compile_nodelist函数进行了实例化,代码如下:
def compile_nodelist(self):
"""
Parse and compile the template source into a nodelist. If debug
is True and an exception occurs during parsing, the exception is
is annotated with contextual line information where it occurred in the
template source.
"""
if self.engine.debug:
lexer = DebugLexer(self.source)
else:
lexer = Lexer(self.source)
tokens = lexer.tokenize()
parser = Parser(
tokens, self.engine.template_libraries, self.engine.template_builtins,
self.origin,
)
实例化语句为 parser = Parser(tokens, self.engine.template_libraries, self.engine.template_builtins,self.origin,),导入了名为engine的已经实例化的类的两个列表,self.engine在该函数,即compile_nodelist函数的父类Template中进行了初始化,即self.engine = engine,Template的初始化语句如下:
class Template:
def __init__(self, template_string, origin=None, name=None, engine=None):
# If Template is instantiated directly rather than from an Engine and
# exactly one Django template engine is configured, use that engine.
# This is required to preserve backwards-compatibility for direct use
# e.g. Template('...').render(Context({...}))
if engine is None:
from .engine import Engine
engine = Engine.get_default()
if origin is None:
origin = Origin(UNKNOWN_SOURCE)
self.name = name
self.origin = origin
self.engine = engine
self.source = str(template_string) # May be lazy.
self.nodelist = self.compile_nodelist()
从from .engine import Engine语句可以看到,这里是引用了engine.py里面的Engine类,,engine.py的路径与defaulttags.py路径相同,其中Engine类的初始化代码如下:
class Engine:
default_builtins = [
'django.template.defaulttags',
'django.template.defaultfilters',
'django.template.loader_tags',
]
def __init__(self, dirs=None, app_dirs=False, context_processors=None,
debug=False, loaders=None, string_if_invalid='',
file_charset='utf-8', libraries=None, builtins=None, autoescape=True):
if dirs is None:
dirs = []
if context_processors is None:
context_processors = []
if loaders is None:
loaders = ['django.template.loaders.filesystem.Loader']
if app_dirs:
loaders += ['django.template.loaders.app_directories.Loader']
if not debug:
loaders = [('django.template.loaders.cached.Loader', loaders)]
else:
if app_dirs:
raise ImproperlyConfigured(
"app_dirs must not be set when loaders is defined.")
if libraries is None:
libraries = {}
if builtins is None:
builtins = []
self.dirs = dirs
self.app_dirs = app_dirs
self.autoescape = autoescape
self.context_processors = context_processors
self.debug = debug
self.loaders = loaders
self.string_if_invalid = string_if_invalid
self.file_charset = file_charset
self.libraries = libraries
self.template_libraries = self.get_template_libraries(libraries)
self.builtins = self.default_builtins + builtins
self.template_builtins = self.get_template_builtins(self.builtins)
Engine在此处没有实例化,这里只是对默认标签、过滤器进行了导入,例如开头的default_builtins列表
default_builtins = [
'django.template.defaulttags',
'django.template.defaultfilters',
'django.template.loader_tags',
]
default_builtins 列表内容作为内建标签和过滤器在self.builtins = self.default_builtins + builtins语句处进行了添加。
我们在查找一下对Engine类进行引用的地方,在路径/root/.virtualenvs/django/lib/python3.7/site-packages/django/template/backends中(在名为django的python虚拟环境下,/root/.virtualenvs为虚拟环境的根目录)的django.py文件中DjangoTemplates类对Engine类进行了实例化,其初始化函数代码如下:
class DjangoTemplates(BaseEngine):
app_dirname = 'templates'
def __init__(self, params):
params = params.copy()
options = params.pop('OPTIONS').copy()
options.setdefault('autoescape', True)
options.setdefault('debug', settings.DEBUG)
options.setdefault('file_charset', settings.FILE_CHARSET)
libraries = options.get('libraries', {})
options['libraries'] = self.get_templatetag_libraries(libraries)
super().__init__(params)
self.engine = Engine(self.dirs, self.app_dirs, **options)
其中的options参数来自于django项目的配置文件settings.py:
TEMPLATES = [
{
'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',
],
'libraries': {
'news_filters': 'app.news.templatetags.news_filters',
'payinfo_filters': 'app.payinfo.templatetags.payinfo_filters',
},
},
},
]
从下面的代码这里对libraries进行了赋值
libraries = options.get('libraries', {})
options['libraries'] = self.get_templatetag_libraries(libraries)
首先从上文的TEMPLATES列表读取自定义的libraries字典,然后调用get_templatetag_libraries对获取的libraries字典进行处理,处理完成后添加options列表作为engine的实例化语句
self.engine = Engine(self.dirs, self.app_dirs, **options)的参数,到此libraries终于得到了赋值。
我们继续往下看,get_templatetag_libraries函数与DjangoTemplates类位于同一个.py文件中,代码如下:
def get_templatetag_libraries(self, custom_libraries):
"""
Return a collation of template tag libraries from installed
applications and the supplied custom_libraries argument.
"""
libraries = get_installed_libraries()
libraries.update(custom_libraries)
return libraries
可以看到上文的libraries在get_templatetag_libraries函数中是作为custom_libraries进行添加,再看get_installed_libraries()函数(位于同一.py文件中),代码如下
def get_installed_libraries():
"""
Return the built-in template tag libraries and those from installed
applications. Libraries are stored in a dictionary where keys are the
individual module names, not the full module paths. Example:
django.templatetags.i18n is stored as i18n.
"""
import os
libraries = {}
candidates = ['django.templatetags']
candidates.extend(
'%s.templatetags' % app_config.name
for app_config in apps.get_app_configs())
for candidate in candidates:
try:
pkg = import_module(candidate)
except ImportError:
# No templatetags package defined. This is safe to ignore.
continue
if hasattr(pkg, '__path__'):
for name in get_package_libraries(pkg):
libraries[name[len(candidate) + 1:]] = name
return libraries
这个函数与DjangoTemplates类位于同一个.py文件,是对libraries进行实际赋值的函数,从解释语句来看,这个函数返回的是内建的template tag libraries(体现在candidates = ['django.templatetags']语句),以及用户自己安装的app下的template tag libraries,即settings.py中的INSTALLED_APPS列表中加载的app,for app_config in apps.get_app_configs()语句是从加载的app中取得app配置参数,app的具体的加载函数和配置方法位于/root/.virtualenvs/django/lib/python3.7/site-packages/django/apps路径下的registry.py文件中的(函数名为populate),有兴趣的可以看一下。获取的app配置参数app_config.name即使所加载的app的名称,例如我的settings.py中的INSTALLED_APPS
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app.cms',
'app.xfzauth',
'app.news',
'app.course',
'app.ueditor',
'app.payinfo',
]
除了默认的文件外,app_config.name的值就是app.cms、app.xfzauth、app.news......等等,candidates.extend()函数则是将这些名称均加上.templatetags后缀,随后的for循环通过轮询app.cms.templatetags、app.news.templatetags等等这些pkg,载入该路径下的tag或者filter(即.py文件),从这里我们也可以看出为什么django自定义app下面的tags和filters的pkg(即包含init.py的文件夹)名称必须是templatetags。
走到这一步应该明白,由于我的news_filters.py过滤器文件位于app.news.templatetags这个pkg下面,既然报的错是news_filters不存在,那么app.news.templatetags肯定没有正常加载,于是我测试了一下,即在for循环中加了两条代码:
for candidate in candidates:
try:
pkg = import_module(candidate)
print(pkg)
except ImportError:
# No templatetags package defined. This is safe to ignore.
print(candidate ,'error')
continue
即对pkg的导入情况进行打印显示,得到结果如下:
<module 'django.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/templatetags/__init__.py'>
<module 'django.contrib.admin.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/contrib/admin/templatetags/__init__.py'>
django.contrib.auth.templatetags error
django.contrib.contenttypes.templatetags error
django.contrib.sessions.templatetags error
django.contrib.messages.templatetags error
<module 'django.contrib.staticfiles.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/contrib/staticfiles/templatetags/__init__.py'>
app.cms.templatetags error
app.xfzauth.templatetags error
app.news.templatetags error
app.course.templatetags error
app.ueditor.templatetags error
app.payinfo.templatetags error
<module 'rest_framework.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/rest_framework/templatetags/__init__.py'>
<module 'debug_toolbar.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/debug_toolbar/templatetags/__init__.py'>
<module 'haystack.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/haystack/templatetags/__init__.py'>
可以看到没有templatetags这个pkg的app加载肯定是失败的(比如app.cms.templatetags、app.xfzauth.templatetags等)但是有templatetags这个pkg的app加载也是失败的(app.news.templatetags和app.payinfo.templatetags),除此之外我还发现python环境自带的(django.templatetags等)和第三方安装(rest_framework.templatetags等)的templatetags是能够正常加载的,这让我很是不解,我一直考虑是Ubuntu和Windows路径查找方式不一样,我也曾尝试将自定义app所在的路径添加到PYTHONPATH环境变量中,也报同样的错。通过上面的方法无法看到报错的pkg到底是从哪一个路径进行pkg载入的,我还试图查看python的import_module函数源代码去找其路径查找方式,无奈能力有限最终放弃,历经千辛万苦我终于想到一个办法,其实也很简单,也是去修改get_installed_libraries函数的for循环语句,只添加了一句:
for candidate in candidates:
print(os.path.abspath(candidate))
try:
pkg = import_module(candidate)
print(pkg)
except ImportError:
print(candidate,'error')
# No templatetags package defined. This is safe to ignore.
continue
print(os.path.abspath(candidate))即打印当前candidate的绝对路径,执行结果如下:
/srv/xfz/app/ueditor/django.templatetags
<module 'django.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/templatetags/__init__.py'>
/srv/xfz/app/ueditor/django.contrib.admin.templatetags
<module 'django.contrib.admin.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/contrib/admin/templatetags/__init__.py'>
/srv/xfz/app/ueditor/django.contrib.auth.templatetags
django.contrib.auth.templatetags error
/srv/xfz/app/ueditor/django.contrib.contenttypes.templatetags
django.contrib.contenttypes.templatetags error
/srv/xfz/app/ueditor/django.contrib.sessions.templatetags
django.contrib.sessions.templatetags error
/srv/xfz/app/ueditor/django.contrib.messages.templatetags
django.contrib.messages.templatetags error
/srv/xfz/app/ueditor/django.contrib.staticfiles.templatetags
<module 'django.contrib.staticfiles.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/contrib/staticfiles/templatetags/__init__.py'>
/srv/xfz/app/ueditor/app.cms.templatetags
app.cms.templatetags error
/srv/xfz/app/ueditor/app.xfzauth.templatetags
app.xfzauth.templatetags error
/srv/xfz/app/ueditor/app.news.templatetags
app.news.templatetags error
/srv/xfz/app/ueditor/app.course.templatetags
app.course.templatetags error
/srv/xfz/app/ueditor/app.ueditor.templatetags
app.ueditor.templatetags error
/srv/xfz/app/ueditor/app.payinfo.templatetags
app.payinfo.templatetags error
/srv/xfz/app/ueditor/rest_framework.templatetags
<module 'rest_framework.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/rest_framework/templatetags/__init__.py'>
/srv/xfz/app/ueditor/debug_toolbar.templatetags
<module 'debug_toolbar.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/debug_toolbar/templatetags/__init__.py'>
/srv/xfz/app/ueditor/haystack.templatetags
<module 'haystack.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/haystack/templatetags/__init__.py'>
从结果可以看到,当前的项目路径由/srv/xfz/变成了/srv/xfz/app/ueditor/,看来这就是原因的所在了,那为什么python环境自带的和第三方安装的templatetags能够正常加载呢,原因应该是/root/.virtualenvs/django/lib/python3.7/site-packages/路径是在PYTHONPATH环境变量中的,如果没有在当前的项目路径的项目路径中找到需要的templatetags(app的加载是一样的),就会到环境变量中去查找。
那么问题来了,为什么项目路径会改变呢,ueditor(一个app)是我加载的一个第三方的富文本编辑器,通过查看其视图函数(view.py)我发现了如下代码:
import json
import re
import string
import time
import hashlib
import random
import base64
import sys
import os
from urllib import parse
from django.conf import settings
from django.http import JsonResponse
from django.shortcuts import reverse
from django.views.decorators.csrf import csrf_exempt
from django.http import FileResponse
from django.views.generic import View
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_http_methods
# 更改工作目录。这么做的目的是七牛qiniu的sdk
# 在设置缓存路径的时候默认会设置到C:/Windows/System32下面
# 会造成没有权限创建。
os.chdir(os.path.dirname(os.path.abspath(__file__)))
很明显,os.chdir这条语句将项目路径由/srv/xfz/变成了/srv/xfz/app/ueditor/,将其注释掉后问题解决。
那么问题又来了,为什么没有访问这个视图函数的url,其代码为什么会执行呢,原因是os.chdir()语句虽然在视图函数中,但并不隶属于任何类和函数,所以在初始化环境是就会执行这条代码,导致项目路径被改变。
那么还是有问题,为什么通过python manage.py runserver方式运行不会报错呢,于是我在os.chdir()语句后面添加了一条语句
print('==========',os.path.dirname(os.path.abspath(__file__)),'=========='),
即打印了执行结果,在Ubuntu中使用uWSGI进行加载,得到的结果如下:
========== /srv/xfz/app/ueditor/app/ueditor ==========
/srv/xfz/app/ueditor/django.templatetags
<module 'django.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/templatetags/__init__.py'>
/srv/xfz/app/ueditor/django.contrib.admin.templatetags
<module 'django.contrib.admin.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/contrib/admin/templatetags/__init__.py'>
/srv/xfz/app/ueditor/django.contrib.auth.templatetags
django.contrib.auth.templatetags error
/srv/xfz/app/ueditor/django.contrib.contenttypes.templatetags
django.contrib.contenttypes.templatetags error
/srv/xfz/app/ueditor/django.contrib.sessions.templatetags
django.contrib.sessions.templatetags error
/srv/xfz/app/ueditor/django.contrib.messages.templatetags
django.contrib.messages.templatetags error
/srv/xfz/app/ueditor/django.contrib.staticfiles.templatetags
<module 'django.contrib.staticfiles.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/django/contrib/staticfiles/templatetags/__init__.py'>
/srv/xfz/app/ueditor/app.cms.templatetags
app.cms.templatetags error
/srv/xfz/app/ueditor/app.xfzauth.templatetags
app.xfzauth.templatetags error
/srv/xfz/app/ueditor/app.news.templatetags
app.news.templatetags error
/srv/xfz/app/ueditor/app.course.templatetags
app.course.templatetags error
/srv/xfz/app/ueditor/app.ueditor.templatetags
app.ueditor.templatetags error
/srv/xfz/app/ueditor/app.payinfo.templatetags
app.payinfo.templatetags error
/srv/xfz/app/ueditor/rest_framework.templatetags
<module 'rest_framework.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/rest_framework/templatetags/__init__.py'>
/srv/xfz/app/ueditor/debug_toolbar.templatetags
<module 'debug_toolbar.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/debug_toolbar/templatetags/__init__.py'>
/srv/xfz/app/ueditor/haystack.templatetags
<module 'haystack.templatetags' from '/root/.virtualenvs/django/lib/python3.7/site-packages/haystack/templatetags/__init__.py'>
我又在Windows环境下通过runserver的方式运行了一遍,得到如下结果:
D:\python\Django\xfz\django.templatetags
<module 'django.templatetags' from 'D:\\python\\virtualenv\\django\\lib\\site-packages\\django\\templatetags\\__init__.py'>
D:\python\Django\xfz\django.contrib.admin.templatetags
<module 'django.contrib.admin.templatetags' from 'D:\\python\\virtualenv\\django\\lib\\site-packages\\django\\contrib\\admin\\templatetags\\__init__.py'>
D:\python\Django\xfz\django.contrib.auth.templatetags
django.contrib.auth.templatetags error
D:\python\Django\xfz\django.contrib.contenttypes.templatetags
django.contrib.contenttypes.templatetags error
D:\python\Django\xfz\django.contrib.sessions.templatetags
django.contrib.sessions.templatetags error
D:\python\Django\xfz\django.contrib.messages.templatetags
django.contrib.messages.templatetags error
D:\python\Django\xfz\django.contrib.staticfiles.templatetags
<module 'django.contrib.staticfiles.templatetags' from 'D:\\python\\virtualenv\\django\\lib\\site-packages\\django\\contrib\\staticfiles\\templatetags\\__init__.py'>
D:\python\Django\xfz\app.cms.templatetags
app.cms.templatetags error
D:\python\Django\xfz\app.xfzauth.templatetags
app.xfzauth.templatetags error
D:\python\Django\xfz\app.news.templatetags
<module 'app.news.templatetags' from 'D:\\python\\Django\\xfz\\app\\news\\templatetags\\__init__.py'>
D:\python\Django\xfz\app.course.templatetags
app.course.templatetags error
D:\python\Django\xfz\app.ueditor.templatetags
app.ueditor.templatetags error
D:\python\Django\xfz\app.payinfo.templatetags
<module 'app.payinfo.templatetags' from 'D:\\python\\Django\\xfz\\app\\payinfo\\templatetags\\__init__.py'>
D:\python\Django\xfz\rest_framework.templatetags
<module 'rest_framework.templatetags' from 'D:\\python\\virtualenv\\django\\lib\\site-packages\\rest_framework\\templatetags\\__init__.py'>
D:\python\Django\xfz\debug_toolbar.templatetags
<module 'debug_toolbar.templatetags' from 'D:\\python\\virtualenv\\django\\lib\\site-packages\\debug_toolbar\\templatetags\\__init__.py'>
D:\python\Django\xfz\haystack.templatetags
<module 'haystack.templatetags' from 'D:\\python\\virtualenv\\django\\lib\\site-packages\\haystack\\templatetags\\__init__.py'>
========== D:\python\Django\xfz\app\ueditor ==========
可见print语句执行的顺序并不一样,Windows下在执行完所有加载之后才执行ueditor下的语句,这就说明了为什么通过runserver的方式没有报错。
总结
这次的错误不算是一个普遍性的错误,能遇到这样的错误也算是运气好,不过这也比买彩票的几率高点。此次纠错让我对python的module加载方式有了新的理解,对runserver和uWSGI的运行机制有了新的认识,本来的话通过修改app的路径也能暂时性的解决一下这个问题,但我总觉得这样没有从根本上解决问题,所以虽然解决这个问题花了点时间,但我觉得还是学到了一些东西,在这里总结一下,希望给大家一个参考。
网友评论