在Django模板标记库中导入外部库时出错
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. :)
所以我正在尝试编写一个Django可重用的应用程序,它提供了一种在页面上显示Twitter提要的方法。我知道它已经存在了20次。这是一次学术练习。 :)
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.
运行Django shell时,twitterfeed.py中定义的函数完美运行。我也相信我有正确命名和注册的模板标签。
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:
如您所见,我使用了优秀的Universal Feed Parser。我的问题不在UFP本身,而是UFP在导入模板标签库时无法调用。当我在base.py中{%load twitterfeed%}时,出现以下错误:
'twitterfeed' is not a valid tag library: Could not load template library from django.templatetags.twitterfeed, No module named feedparser
'twitterfeed'不是有效的标记库:无法从django.templatetags.twitterfeed加载模板库,没有名为feedparser的模块
I import feedparser using the following statement:
我使用以下语句导入feedparser:
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.
我能说的最好,这个错误信息有点欺骗。我认为加载模板库时会发生ImportError,这是Django对它的解释。
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?
有没有什么办法可以在我的可重用应用程序中导入feedparser.py,而无需让应用程序的用户在其PythonPath中的某处放置feedparser?
Thanks!
2 个解决方案
#1
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
这看起来像是烦人的相对路径问题之一 - 在Python 2.6及更高版本中解决(你可以在其中导入..feedparser等)但在旧版本上经常有点棘手。解决这个问题的一种便宜而愉快的方法就是将feedparser.py移动到你的templatetags目录中,作为twitterfeed.py的兄弟
更多相关文章
- 使用Python写HTML 文件使用jinja2中的模板
- 在两台服务器上有效地在两个Django应用程序之间进行通信(多租户)
- 如何停止Py_Initialise应用程序的崩溃?
- 轮询Web服务的最佳方式(例如,对于Twitter应用程序)
- django模板引擎有错误检查?
- 乘客的Django应用程序显示空主页
- 用于在Google App Engine上构建应用程序的最佳开源IDE?
- 获取Android应用程序的Linux UID
- 在Linux中的脚本中安装应用程序