Django admin中出现“ImportError:没有名为urls的模块”

时间:2012-12-22 12:01:07

标签: python django nginx django-admin uwsgi

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上评论admin,然后就不会发生错误。

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

我该如何解决?

urls.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

settings.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',
)

1 个答案:

答案 0 :(得分:0)

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

我通过为其设置PROJECT_HOME环境变量来配置我的uwsgi初始化文件,该变量在我的进程用户的.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.ini配置中所看到的那样,VIRTUAL_ENV目录也包含在PYTHONPATH中1}}用于uwsgi进程。

要运行我的uwsgi进程,就像运行此命令一样简单: -

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

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