Django与Python + uWSGI

7 投票
2 回答
11395 浏览
提问于 2025-04-16 04:29

我在使用这个教程,想把PythonuWSGI连接起来。

我在/home/sanya/django/pasteurl这个文件夹里创建了一个默认的项目。

但是,当我在浏览器中打开它时,出现了以下内容:

uWSGI Error
wsgi application not found

日志里包含了以下信息:

binding on TCP port: 9001
your server socket listen backlog is limited to 64 connections
added /home/sanya/django/pasteurl to pythonpath.
initializing hooks...done.
...getting the applications list from the 'django' module...
uwsgi.applications dictionary is not defined, trying with the "applications" one...
applications dictionary is not defined, trying with the "application" callable.
static applications not defined, you have to use the dynamic one...
spawned uWSGI master process (pid: 7637)
spawned uWSGI worker 1 (pid: 7646)
spawned uWSGI worker 2 (pid: 7647)
spawned uWSGI worker 3 (pid: 7648)
spawned uWSGI worker 4 (pid: 7649)

文件/home/sanya/django/pasteurl/django.wsgi

import os
import django.core.handlers.wsgi

# init django settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'pasteurl.settings'

# define wsgi app
application = django.core.handlers.wsgi.WSGIHandler()

# mount this application at the webroot
# applications = { '/': 'application' }

我意识到,这个application字典可能有问题。

2 个回答

1

我也遇到同样的问题,这里补充一下:

uWSGI Error
wsgi application not found

检查一下nginx的配置:

uwsgi_param UWSGI_CHDIR  somepath
wsgi_param UWSGI_SCRIPT  somefile

确保以下几点:

1. sompath/somefile.py 这个文件必须存在

2. 文件名后缀必须是 ".py"

3. 不要使用完整的文件名,比如 "somefile.py",否则会出现同样的错误,并且在uwsgi的日志文件中会有错误记录:

ImportError: No module named py
1

顺便说一下,看看这个源代码,从1997行开始,我们可以看到,如果uWSGI找不到一个applications字典,它会发出和你收到的错误信息完全一样的提示。

再看看你的django.wsgi文件,我们发现,

`applications = {'/': 'application'} 

这一行是被注释掉的。我在想,我们能对此做些什么呢;)

顺便提一下,我是通过谷歌找到这个源代码的。搜索带引号的错误信息通常是个不错的主意。在我点击那个链接并意识到我很幸运直接从谷歌找到了源代码(这种情况越来越多),我按下了Ctrl-F来在页面上搜索,然后把错误信息重新输入到浏览器的搜索框里,这样就直接找到了相关的代码行。

撰写回答