1

Django:1.3.5
nginx:1.2.6
uwsgi:1.0.5

我的 django 应用程序在 nginx 和 uwsgi 上运行。打开管理页面时,出现 500 错误。此错误是“ImportError:没有名为 urls 的模块”。

运行命令“python manage.py runserver”并打开管理页面,然后没有出现500错误。

在 urls.py 上评论管理员,然后不会发生错误。

此错误仅发生在 nginx 和 uwsgi 上。

我该如何解决?

网址.py

from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
                       (r'^blog/', include('myApp.blog.urls')),
                       (r'^admin/', include(admin.site.urls))
                       )

wsgi.py

import os
import sys
import site

reload(sys)
sys.setdefaultencoding('utf-8')
sys.path.append('/path/to/myApp/')

site.addsitedir('/usr/lib/python2.6/site-packages')
os.environ['DJANGO_SETTINGS_MODULE'] = 'myApp.settings'

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

错误回溯

Traceback (most recent call last):

File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

File "/usr/lib/python2.6/site-packages/django/contrib/admin/sites.py", line 214, in wrapper
    return self.admin_view(view, cacheable)(*args, **kwargs)

File "/usr/lib/python2.6/site-packages/django/utils/decorators.py", line 93, in _wrapped_view
    response = view_func(request, *args, **kwargs)

File "/usr/lib/python2.6/site-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)

File "/usr/lib/python2.6/site-packages/django/contrib/admin/sites.py", line 197, in inner
    return view(request, *args, **kwargs)

File "/usr/lib/python2.6/site-packages/django/views/decorators/cache.py", line 79, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)

File "/usr/lib/python2.6/site-packages/django/contrib/admin/sites.py", line 382, in index
    context_instance=context_instance

File "/usr/lib/python2.6/site-packages/django/shortcuts/__init__.py", line 20, in render_to_response
    return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

File "/usr/lib/python2.6/site-packages/django/template/loader.py", line 188, in render_to_string
    return t.render(context_instance)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 123, in render
    return self._render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 117, in _render
    return self.nodelist.render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 757, in render_node
    return node.render(context)

File "/usr/lib/python2.6/site-packages/django/template/loader_tags.py", line 127, in render
    return compiled_parent._render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 117, in _render
    return self.nodelist.render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 757, in render_node
    return node.render(context)

File "/usr/lib/python2.6/site-packages/django/template/loader_tags.py", line 127, in render
    return compiled_parent._render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 117, in _render
    return self.nodelist.render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 757, in render_node
    return node.render(context)

File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py", line 311, in render
    return self.nodelist_true.render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 757, in render_node
    return node.render(context)

File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py", line 311, in render
    return self.nodelist_true.render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 757, in render_node
    return node.render(context)

File "/usr/lib/python2.6/site-packages/django/template/loader_tags.py", line 64, in render
    result = block.nodelist.render(context)

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 744, in render
    bits.append(self.render_node(node, context))

File "/usr/lib/python2.6/site-packages/django/template/base.py", line 757, in render_node
    return node.render(context)

File "/usr/lib/python2.6/site-packages/django/template/defaulttags.py", line 437, in render
    url = reverse(view_name, args=args, kwargs=kwargs, current_app=context.current_app)

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 391, in reverse
    *args, **kwargs)))

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 312, in reverse
    possibilities = self.reverse_dict.getlist(lookup_view)

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 229, in _get_reverse_dict
    self._populate()

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 208, in _populate
      For name in pattern.reverse_dict:

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 229, in _get_reverse_dict
    self._populate()

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 208, in _populate
      For name in pattern.reverse_dict:

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 229, in _get_reverse_dict
    self._populate()

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 197, in _populate
      For pattern in reversed(self.url_patterns):

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 279, in _get_url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)

File "/usr/lib/python2.6/site-packages/django/core/urlresolvers.py", line 274, in _get_urlconf_module
    self._urlconf_module = import_module(self.urlconf_name)

File "/usr/lib/python2.6/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)

ImportError: No module named urls

设置.py (INSTALLED_APPS)

ROOT_URLCONF = 'myApp.urls'
INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.admin',
    'django.contrib.sitemaps',
    'django.contrib.markup',
    'myApp.blog',
)
4

1 回答 1

0

你如何开始你的 uwsgi 进程?您的 uwsgi 进程需要从项目根目录的位置启动。

我通过给它一个环境变量来配置我的 uwsgi 初始化文件,该PROJECT_HOME环境变量在我的进程用户.bash_profile文件中定义。

在我的流程用户中.bash_profile,我有:-

export PROJECT_HOME=/var/www/myproject

然后,在我的 uwsgi 初始化文件中,我有:-

[uwsgi]

if-env = PROJECT_HOME
chdir = %(_)
touch-reload = %(_)/uwsgi.ini
daemonize = %(_)/myproject.log
endif =

if-env = VIRTUAL_ENV
virtualenv = %(_)
endif = 

socket = 127.0.0.1:3030
pidfile=/tmp/myproject.pid-3030
processes = 4
module = django.core.handlers.wsgi:WSGIHandler()
env = DJANGO_SETTINGS_MODULE=myproject.settings
master = True
vacuum = True
max-requests = 5000
logdate = True

enable-threads = True
single-interpreter = True

该文件chdir中的配置uwsgi.ini告诉我uwsgi的进程从该目录启动该进程并将该目录包含在其中,PYTHONPATH以便可以找到我的项目的模块。

如果您还VIRTUAL_ENV定义了一个环境,正如您从我在文件VIRTUAL_ENV中的配置中看到的那样,该目录也包含在该进程中。uwsgi.iniVIRTUAL_ENVPYTHONPATHuwsgi

要运行我的 uwsgi 进程,只需运行以下命令即可:-

uwsgi --ini /var/www/myproject/uwsgi.ini

urls.py这可确保您的 uwsgi 进程正确加载您的所有 python 模块(例如您的)。希望这可以帮助!

于 2012-12-30T00:30:54.437 回答