使用 mod_wsgi 运行 Django 教程示例?

发布于 2024-10-04 06:29:44 字数 2313 浏览 2 评论 0原文

我安装了 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 示例?

我发现这个示例有一个简单的例子。

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 生成了三个 python 文件,我看到一个包含设置信息的 settings.py。 该项目位于“/ABC/DEF/mysite”目录中。

我将代码修改如下,并将其命名为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()

尽管我可以从使用“python”的 教程 运行原始 django 示例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

据我所知,目录/项目名称是mysite,它有settings.py。并且该目录位于sys.path中。所以,我不知道为什么找不到 mysite.settings 。

我的 myapp.wsgi 有什么问题吗?

已解决

以下代码工作正常。 我修改的另一件事是 django 项目不应该在我的主目录中,当我将项目移动到 www doc 目录时,一切正常。

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()

I installed wsgi (mod_wsgi), and could run the simple application by calling 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]

How can I run django example?

I found this example that has the simple 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()

Even though I could run the original django example from the tutorial that uses "python manage.py runserver 8080", I got an error running the modified example with mod_wsgi.

The apache2/log file has the following.

[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()

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

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

发布评论

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

评论(1

九八野马 2024-10-11 06:29:44

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

编辑:

“Django 设置”

编辑2:

如何设置 Django设置模块

编辑3:

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

What you need to do is add either the project directory or the directory the project is in to sys.path, and then point the settings to mysite.settings if you've done the latter.

EDIT:

"Django settings"

EDIT 2:

How to set the Django settings module

EDIT 3:

The settings module should be the module, not the filename. And if it's 'settings' then you don't need to set it.

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