在自定义 Django 模板标签中导入 Python 模块

发布于 2024-10-22 01:58:14 字数 2281 浏览 6 评论 0原文

我在 Python Django 安装中使用了 virtualenv。

这是我的目录结构

project/
    dev_environ/
        lib/
            python2.6/
                site-packages/
                    ...
                    django/
                    titlecase/   # <-- The titlecase module
                    PIL/
                    ...
        bin/
            ...
            python  # <-- Python
            ...
        include/

    django_project/
        localsite/
            templatetags/
                __init__.py
                smarttitle.py    # <-- My templatetag module
        foo_app/
        bar_app/
        settings.py
        manage.py

如果我启动 Django shell 并尝试导入 titlecase 一切都很好,因为 titlecase 位于 sys.path 位于 dev_environ/lib/python2.6/site-packages/titlecase

$:django_project cwilcox$ ../dev_environ/bin/python manage.py shell
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import titlecase  # <-- No Import Error!
>>> 

我什至可以在我的 settings.py 文件中执行 import titlecase 而不会出现错误。

但是,当我尝试在模板标签库 smarttitle.py导入 titlecase 时,我收到一个 ImportError

smarttitle.py如下。

from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
from titlecase import titlecase as _to_titlecase

@register.filter
@stringfilter
def smarttitle(value):
    return _to_titlecase(value)
smarttitle.is_safe = True

不仅如此,我什至可以在视图中导入 titlecase ,该视图呈现尝试 {% load smarttitle %} 的模板,并且没有错误。

我的 Django 开发服务器是用...

../dev_environ/bin/python manage.py runserver

总结:

我可以在 任何地方导入 titlecase 模块,除了在此 templatetag 库中,它会抛出异常一个导入错误!什么给?!有什么想法吗?


编辑:我尝试首先运行 source dev_environ/bin/activate 将我的 shell 环境切换到我的 virtualenv,但这没有帮助 - 我仍然收到 ImportError在我的 templatetag 模块中。我已经手动调用了正确的 python 二进制文件。

I'm using virtualenv with my Python Django installation.

Here is my directory structure:

project/
    dev_environ/
        lib/
            python2.6/
                site-packages/
                    ...
                    django/
                    titlecase/   # <-- The titlecase module
                    PIL/
                    ...
        bin/
            ...
            python  # <-- Python
            ...
        include/

    django_project/
        localsite/
            templatetags/
                __init__.py
                smarttitle.py    # <-- My templatetag module
        foo_app/
        bar_app/
        settings.py
        manage.py

If I start my Django shell and attempt to import titlecase everything is fine, because titlecase is in the sys.path at dev_environ/lib/python2.6/site-packages/titlecase.

$:django_project cwilcox$ ../dev_environ/bin/python manage.py shell
Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import titlecase  # <-- No Import Error!
>>> 

I can even do an import titlecase inside my settings.py file without error.

However when I try to import titlecase in my templatetag library smarttitle.py I get an ImportError.

smarttitle.py is as follows.

from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
from titlecase import titlecase as _to_titlecase

@register.filter
@stringfilter
def smarttitle(value):
    return _to_titlecase(value)
smarttitle.is_safe = True

Not only that but I can even import titlecase inside the view that renders the template that tries to {% load smarttitle %} and there's no error.

My Django dev server is started with...

../dev_environ/bin/python manage.py runserver

In Summary:

I can import the titlecase module anywhere except inside this templatetag library, where it throws an ImportError! What gives?! Any ideas?


EDIT: I tried first running source dev_environ/bin/activate to switch my shell env to my virtualenv, but that didn't help--I'm still getting the ImportError inside my templatetag module. I was already calling the proper python binary manually.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千秋岁 2024-10-29 01:58:14

正如评论中所述,在运行 devserver 之前,您需要通过执行 source bin/activate (或只是 .bin/activate)来激活您的 virtualenv,即使您已经访问正确的 Python 可执行文件。

As stated in the comments, you need to activate your virtualenv by doing source bin/activate (or just . bin/activate) before running the devserver, even if you are already accessing the right Python executable.

蒗幽 2024-10-29 01:58:14

我知道这太旧了,但我今天遇到了类似的问题。

问题似乎是应用程序和模块使用相同的名称,因此当它尝试导入时,可能会无法在错误的位置查找所需的模块或函数。

我建议您为 django 应用程序或模块指定不同的名称

I know that this is too old, but I get a similar problem today.

The problem seems to be using the same name for the app and the module, so when it tries to import it could fail looking for the desired module or function in the wrong place.

I recommend you to give different names to the django app or module.

帝王念 2024-10-29 01:58:14

这不是修复,只是为了确定我们正在考虑相同的问题/错误:

如果将 smarttitle.py 中的导入更改为

from YOURPROJECT.titlecase import titlecase as _to_titlecase

它将与“runserver”一起使用,但它将失败生产(在我的例子中是uwsgi/nginx)

This is not a fix, but just to establish that we are looking at the same problem/bug:

If you change the import in smarttitle.py to

from YOURPROJECT.titlecase import titlecase as _to_titlecase

it will work with 'runserver' but it will fail on production (in my case uwsgi/nginx)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文