python - 使用 mod_wsgi 运行 Django 教程示例?

标签 python django mod-wsgi

我安装了 wsgi (mod_wsgi),可以通过调用 http://localhost/myapp 运行简单的应用程序.

WSGIScriptAlias /myapp /Library/WebServer/Documents/wsgi/scripts/myapp.wsgi
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World! WSGI working perfectly!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

如何运行 django 示例?

我找到了 this example有一个简单的例子。

import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'my.settings' # ???

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
mysite/
    __init__.py
    manage.py
    settings.py
    urls.py

Django generated the three python files, and I see a settings.py that has setup info. The project is in '/ABC/DEF/mysite' directory.

I modified the code as follows, and named it myapp.wsgi.

import os
import sys

sys.path.append('/ABC/DEF/mysite') 
sys.path.append('/ABC/DEF')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

即使我可以运行来自 the tutorial 的原始 django 示例使用“python manage.py runserver 8080”,我在使用 mod_wsgi 运行修改后的示例时遇到错误。

apache2/log 文件有以下内容。

[Wed Nov 24 09:02:41 2010] [error] [client 127.0.0.1] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings

As far as I know, the directory/project name is mysite, and it has the settings.py. And the directory is in the sys.path. So, I don't know why mysite.settings is not found.

What's wrong with my myapp.wsgi?

SOLVED

The following code works fine. And the other thing that I modified was the django project should not be in my home directory, when I moved the project to www doc directory, everything works fine.

import os
import sys

mysite = 'SOMEWEHRE/django'
if mysite not in sys.path:sys.path.insert(0,mysite)
mysite = 'SOMEWEHRE/scripts'
if mysite not in sys.path:sys.path.insert(0,mysite)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

最佳答案

您需要做的是将项目目录或项目所在的目录添加到sys.path,然后将设置指向mysite.settings,如果你已经完成了后者。

编辑:

"Django settings"

编辑 2:

How to set the Django settings module

编辑 3:

设置模块应该是模块,而不是文件名。如果它是 'settings' 那么您不需要设置它。

关于python - 使用 mod_wsgi 运行 Django 教程示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4263001/

相关文章:

apache - 无法理解在 ubuntu 上安装 apache2 的正确方法

python - Apache + mod_wsgi + 持久性 Python 应用程序 - 我想我错过了一 block

python - Apache 无法访问 Django 应用程序

python - HTTPSConnectionPool SSL错误证书验证失败

python - Folium & Series 对象没有属性 get_name

python - Models.py 问题( key )

django - 当 drf 使用 ListAPIView 返回空查询集时如何返回 404

python - 以编程方式在 django ModelForm 中设置排除字段的值

python - 使用矢量化在 Pandas 数据框中查找大于当前值的第一次出现的价格值

python - 在python脚本中截取打开网站的屏幕截图