需要帮助解决 sorl-thumbnail 错误:““thumbnail”不是有效的标签库:”
我一直在绞尽脑汁试图解决这个问题,我已经尝试了一切,但我没有任何想法。
我不断看到这个错误: 异常值:“thumbnail”不是有效的标签库:无法从 django.templatetags.thumbnail 加载模板库,不
存在名为 sorl.thumbnail.main $DJANGO_PACKAGES/sorl/thumbnail/main.py 的模块。
这是我所做的设置,
下载最新的 sorl-thumbnail 并将其位置添加到 .bash_profile 中的 python 路径
included INSTALLED_APPS 中的“sorl.thumbnail”(在 django 的 settings.py 中)
在 django 模板中使用了 {% loadthumbnail %} 标记< /p>
看起来很明显 sorl-thumbnail 没有正确安装,但我可以从 python shell 和 django shell 导入缩略图(当我使用 {%加载缩略图%}它会带来这个错误)。另外,相关文件中没有错别字(我已经检查过很多次了)。
I've been pulling my hair out trying to solve this problem and I've tried everything and I have no ideas left.
I keep seeing this error:
Exception Value: 'thumbnail' is not a valid tag library: Could not load template library from django.templatetags.thumbnail, No module named sorl.thumbnail.main
$DJANGO_PACKAGES/sorl/thumbnail/main.py DOES exist.
Here's what I did to setup,
downloaded latest sorl-thumbnail and added its location to the python path in .bash_profile
included 'sorl.thumbnail' in INSTALLED_APPS (in django's settings.py)
used the {% load thumbnail %} tag in a django template
It would seem obvious sorl-thumbnail is not installed correctly, but I'm able to import thumbnail from the python shell and the django shell (when I use {% load thumbnail %} it brings this error). Also, there are no typos in related files (I've checked many many times).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我大胆猜测这是一个 $PYTHONPATH 问题。是否有可能“thumbnail”目录位于路径上而不是“sorl”?我怀疑这是问题所在,因为您不希望能够在 Python 解释器上键入“导入缩略图”。您应该输入“import sorl.thumbnail”。
另一件要检查的事情是在导入后打印模块名称:
这将显示找到该模块的文件系统位置,以防它从您不希望的地方加载另一个副本。
您还需要确保当前工作目录不是根 ../sorl/ 位置(即不要从 sorl 文件夹运行 python)。这将允许您立即导入缩略图。
您应该从 python 解释器中检查完整的 Python 路径(它将超过 $PYTHONPATH)以验证您的包位置:
了解有关 Python 导入
I would venture to guess this is a $PYTHONPATH issue. Is it possible that the "thumbnail" directory is on the path and not "sorl"? I suspect this is the issue because you do not want to be able to type "import thumbnail" on the Python interpreter. You should instead have to type "import sorl.thumbnail".
Another thing to check is to print the module name after importing:
This will display the filesystem location where the module was found, in case it's loading another copy from somewhere you do not expect.
You also want to make sure your current working directory is not the root ../sorl/ location (ie. don't run python from the sorl folder). This will allow you to import thumbnail straight-away.
You should check your full Python path (it will be more than $PYTHONPATH) from within the python interpreter to verify your package locations:
It might also be helpful to learn more about Python importing
问题解决了。
在学习 django 书籍时,建议在项目目录中创建应用程序,并使用从包含项目的目录开始的路径(例如“siteproject.books”)在 INSTALLED APPS 语句中引用这些应用程序。如果不将该目录名称附加到文件路径,我无法让 django 访问应用程序,因此,例如,我无法简单地使用“books”,而是需要在 INSTALLED APPS 语句中使用“siteproject.books” sorl.thumbnail 就是这种情况,需要将其称为 siteproject.sorl.thumbnail。其他包含“sorl.thumbnail”的尝试将产生一个非常丑陋的未格式化且令人困惑的紫色错误页面(是的,sorl 目录位于 $PYTHONPATH 中,所以谁知道为什么这些尝试不起作用......)。
不幸的是,Django 产生了“未定义标签”错误,这是在许多情况下都会出现的普遍错误。它实际上没有任何意义,对于定位问题没有什么用处。
当我打开 sorl 目录中的文件并编辑 python 文件时,问题就解决了。我找到了从 sorl 目录导入对象的 import 语句,并将“siteproject.*”附加到这些路径中,一切都开始工作。
Problem solved.
When following the django book, it is suggested to create apps within a project directory and to refer to these apps in the INSTALLED APPS statement with a path that begins from the directory containing the project, for example, 'siteproject.books'. I was not able to give django access to apps without appending that directory name to the file path, so, for example, I was not able to simply use 'books', but needed to use 'siteproject.books' in the INSTALLED APPS statement and this was the case with sorl.thumbnail, which needed to be referred to as siteproject.sorl.thumbnail. Other attempts to include 'sorl.thumbnail' would yield a very ugly un-formatted and confusing purple-colored error page (yes, the sorl directory was in $PYTHONPATH, so who knows why these attempts didn't work...).
Unfortunately, Django was yielding the 'undefined tag' error, which is a generalized error that it gives in many situations. It doesn't really mean anything and isn't useful for locating problems.
The problem was solved when I opened the files in the sorl directory and edited the python files. I found import statements that imported objects from the sorl directory and I appended the 'siteproject.*' to those paths and everything began to work.
这是关于无用的“不是有效标签库”消息的另一个一般提示:对于您创建的标签,它可能就像语法错误一样简单。
帽子提示:Django 用户的“摇滚”:http ://groups.google.com/group/django-users/browse_thread/thread/d65db3940acf16c3?tvc=2
Here's another general tip on the unhelpful 'not a valid tag library' message: for tags that you create, it could be as simple as a syntax error.
Hat tip: 'Rock' on Django-users: http://groups.google.com/group/django-users/browse_thread/thread/d65db3940acf16c3?tvc=2