【发布时间】:2012-09-19 01:45:00
【问题描述】:
我正在尝试在 Django 中使用 BeautifulSoup 版本 4 动态呈现 HTML 页面(使用 Apache2 和 mod_python)。 但是,只要我将任何 HTML 字符串传递给 BeautifulSoup 构造函数(参见下面的代码),浏览器就会挂起等待网络服务器。我在 CLI 中尝试了等效代码,它就像一个魅力。所以我猜这与 BeautifulSoups 环境有关,在这种情况下是 Django + Apache + mod_python。
import bs4
import django.shortcuts as shortcuts
def test(request):
s = bs4.BeautifulSoup('<b>asdf</b>')
return shortcuts.render_to_response('test.html', {})
我已经使用 pip pip install beautifulsoup4 安装了 BeautifulSoup。我尝试使用标准 Debian 软件包 apt-get install python-beautifulsoup 安装 BeautifulSoup3,然后以下等效代码可以正常工作(来自浏览器和 CLI)。
from BeautifulSoup import BeautifulSoup
import django.shortcuts as shortcuts
def test(request):
s = BeautifulSoup('<b>asdf</b>')
return shortcuts.render_to_response('test.html', {})
我查看了 Apache 的访问和错误日志,但它们没有显示任何信息显示停止的请求发生了什么。我还检查了 /var/log/syslog 和 /var/log/messages,但没有更多信息。
这是我使用的 Apache 配置:
<VirtualHost *:80>
DocumentRoot /home/nandersson/src
<Directory /home/nandersson/src>
SetHandler python-program
PythonHandler django.core.handlers.modpython
SetEnv DJANGO_SETTINGS_MODULE app.settings
PythonOption django.root /home/nandersson/src
PythonDebug On
PythonPath "['/home/nandersson/src'] + sys.path"
</Directory>
<Location "/media/">
SetHandler None
</Location>
<Location "/app/poc/">
SetHandler None
</Location>
</VirtualHost>
我不确定如何进一步调试,不确定它是否是错误。 有没有人知道如何解决这个问题或遇到过类似的问题?
【问题讨论】:
-
向我们展示您使用的 apache 配置。以及 wsgi.py 文件的内容。
-
我更新了这个,谢谢。但是我使用的是前面提到的 mod_python,所以没有 wsgi.py 可以显示。
-
完全不相关,但你应该像这样导入你的包:
from django import shorcuts,因为你使用的是 python2.7
标签: python django apache beautifulsoup mod-python