在 Django 模板标签库中导入外部库时出错
因此,我尝试编写一个 Django 可重用应用程序,它提供了一种在页面上显示 Twitter feed 的方法。 我很清楚它已经存在了 20 次。 这是一项学术练习。 :)
目录结构非常简单:
myproject
|__ __init__.py
|__ manage.py
|__ settings.py
|__ myapp
|__ __init__.py
|__ admin.py
|__ conf
|__ __init__.py
|__ appsettings.py
|__ feedparser.py
|__ models.py
|__ templates
|__ __init__.py
|__ templatetags
|__ __init__.py
|__ twitterfeed.py
|__ views.py
|__ templates
|__ base.html
|__ urls.py
运行 Django shell 时,twitterfeed.py 中定义的函数可以完美运行。 我还相信我已经正确命名和注册了模板标签。
正如您所看到的,我使用了出色的通用 Feed 解析器。 我的问题不在于UFP本身,而是在于导入模板标签库时UFP无法被调用。 当我在 base.py 中 {% load twitterfeed %}
时,出现以下错误:
“twitterfeed”不是有效标签 库:无法加载模板 图书馆来自 django.templatetags.twitterfeed,否 名为 feedparser 的模块
我使用以下语句导入 feedparser:
import re, datetime, time, myapp.feedparser
据我所知,此错误消息有点欺骗性。 我认为加载模板库时会发生 ImportError ,这是 Django 对它的解释。
有什么方法可以在我的可重用应用程序中导入 feedparser.py ,而不要求应用程序的用户将 feedparser 放置在其 PythonPath 中的某个位置?
谢谢!
So I'm attempting to write a Django reusable app that provides a method for displaying your Twitter feed on your page. I know well that it already exists 20 times. It's an academic exercise. :)
Directory structure is pretty simple:
myproject
|__ __init__.py
|__ manage.py
|__ settings.py
|__ myapp
|__ __init__.py
|__ admin.py
|__ conf
|__ __init__.py
|__ appsettings.py
|__ feedparser.py
|__ models.py
|__ templates
|__ __init__.py
|__ templatetags
|__ __init__.py
|__ twitterfeed.py
|__ views.py
|__ templates
|__ base.html
|__ urls.py
When running the Django shell, the functions defined in twitterfeed.py work perfectly. I also believe that I have the template tags properly named and registered.
As you can see, I use the excellent Universal Feed Parser. My problem is not within UFP itself, but in UFP's inability to be called while importing the template tag library. When I {% load twitterfeed %}
in base.py, I get the following error:
'twitterfeed' is not a valid tag
library: Could not load template
library from
django.templatetags.twitterfeed, No
module named feedparser
I import feedparser using the following statement:
import re, datetime, time, myapp.feedparser
The best I can tell, this error message is slightly deceiving. I think there's an ImportError going on when the template library is loaded, and this is Django's interpretation of it.
Is there any way I can import feedparser.py within my reusable app without requiring users of the app to place feedparser somewhere in their PythonPath?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过以下方式解决此类问题(运送作为我的整个项目的依赖项的库)。 首先,我在项目的根目录中创建一个“ext”目录(在您的情况下,该目录为
myproject/ext
)。 然后,我将 feedparser 等依赖项放置在 ext 目录中 -myproject/ext/feedparser
最后,我更改 manage.py 脚本,将 ext/ 目录插入到 sys.path 的前面。 这意味着
./manage.py runserver
和./manage.py shell
都会选择正确的路径:如果您不想,我发现这非常有效搞乱像 virtualenvs 这样的东西。 当您部署项目时,您还必须确保路径正确 - 我通常通过将相同的 sys.path.insert 行添加到 mod_wsgi app.wsgi 文件的开头来解决此问题。
I solve this kind of problem (shipping libraries that are dependencies for my overall project) in the following way. First, I create an "ext" directory in the root of my project (in your case that would be
myproject/ext
). Then I place dependencies such as feedparser in that ext directory -myproject/ext/feedparser
Finally, I change my manage.py script to insert the ext/ directory at the front of sys.path. This means both
./manage.py runserver
and./manage.py shell
will pick up the correct path:I find this works really well if you don't want to mess around with things like virtualenvs. When you deploy your project you have to make sure the path is correct as well - I usually solve this by adding the same
sys.path.insert
line to the start of my mod_wsgi app.wsgi file.这看起来像是那些烦人的相对路径问题之一 - 在 Python 2.6 及更高版本中已解决(您可以在其中导入 ..feedparser 等),但在旧版本上通常有点棘手。 解决此问题的一种廉价且令人愉快的方法可能是将 feedparser.py 移动到 templatetags 目录中,作为 twitterfeed.py 的同级目录
This looks like one of those annoying relative path issues - solved in Python 2.6 and higher (where you can do import ..feedparser etc) but often a bit tricky on older versions. One cheap and cheerful way to fix this could be just to move feedparser.py in to your templatetags directory, as a sibling to twitterfeed.py