1

我最近为一个学校项目安装了带有 Apache、Python 和 Django 的 Ubuntu。我在实际启动和运行它时遇到了问题。我跟着这个教程

一切都如图所示。我得到了默认的“它工作!” 页。当我开始为该项目实际添加我的应用程序时,我的问题就出现了。我收到以下错误:

[Tue Apr 02 18:20:27 2013] [error] Internal Server Error: /login
[Tue Apr 02 18:20:27 2013] [error] Traceback (most recent call last):
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 92, in get_response
[Tue Apr 02 18:20:27 2013] [error]     response = middleware_method(request)
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/middleware/common.py", line 69, in process_request
[Tue Apr 02 18:20:27 2013] [error]     if (not urlresolvers.is_valid_path(request.path_info, urlconf) and
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 551, in is_valid_path
[Tue Apr 02 18:20:27 2013] [error]     resolve(path, urlconf)
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 440, in resolve
[Tue Apr 02 18:20:27 2013] [error]     return get_resolver(urlconf).resolve(path)
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 319, in resolve
[Tue Apr 02 18:20:27 2013] [error]     for pattern in self.url_patterns:
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 347, in url_patterns
[Tue Apr 02 18:20:27 2013] [error]     patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/urlresolvers.py", line 342, in urlconf_module
[Tue Apr 02 18:20:27 2013] [error]     self._urlconf_module = import_module(self.urlconf_name)
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
[Tue Apr 02 18:20:27 2013] [error]     __import__(name)
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/pumpit2/pumpitproj/pumpitproj/urls.py", line 20, in <module>
[Tue Apr 02 18:20:27 2013] [error]     url(r'^login/', include('pumpitproj.loginhandler.urls')),
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 25, in include
[Tue Apr 02 18:20:27 2013] [error]     urlconf_module = import_module(urlconf_module)
[Tue Apr 02 18:20:27 2013] [error]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
[Tue Apr 02 18:20:27 2013] [error]     __import__(name)
[Tue Apr 02 18:20:27 2013] [error] ImportError: No module named loginhandler.urls
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx] mod_wsgi (pid=18637): Exception occurred processing WSGI script '/srv/pumpit/pumpit2/conf/apache/django.wsgi'.
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx] Traceback (most recent call last):
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 255, in __call__
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     response = self.get_response(request)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 178, in get_response
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 217, in handle_uncaught_exception
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return debug.technical_500_response(request, *exc_info)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/views/debug.py", line 69, in technical_500_response
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     html = reporter.get_traceback_html()
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/views/debug.py", line 298, in get_traceback_html
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return t.render(c)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/template/base.py", line 140, in render
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return self._render(context)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return self.nodelist.render(context)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/template/base.py", line 830, in render
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     bit = self.render_node(node, context)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return node.render(context)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/template/debug.py", line 84, in render
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     output = self.filter_expression.resolve(context)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/template/base.py", line 606, in resolve
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     new_obj = func(obj, *arg_vals)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/template/defaultfilters.py", line 715, in date
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return format(value, arg)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/dateformat.py", line 312, in format
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return df.format(format_string)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/dateformat.py", line 35, in format
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     pieces.append(force_text(getattr(self, piece)()))
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/dateformat.py", line 216, in r
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return self.format('D, j M Y H:i:s O')
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/dateformat.py", line 35, in format
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     pieces.append(force_text(getattr(self, piece)()))
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/encoding.py", line 99, in force_text
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     s = s.__unicode__()
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/functional.py", line 130, in __text_cast
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return func(*self.__args, **self.__kw)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 71, in ugettext
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return _trans.ugettext(message)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 276, in ugettext
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     return do_translate(message, 'ugettext')
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 258, in do_translate
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     _default = translation(settings.LANGUAGE_CODE)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 172, in translation
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     default_translation = _fetch(settings.LANGUAGE_CODE)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 154, in _fetch
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     app = import_module(appname)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]   File "/srv/pumpit/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx]     __import__(name)
[Tue Apr 02 18:20:27 2013] [error] [client xx.xxx.xxx.xxx] ImportError: No module named loginhandler

我的目录设置如下:

+ srv
---+ pumpit
------+ pumpit2
---------+ conf
------------+ apache
---------+ pumpitproj
------------+ loginhandler
------------+ main
------------+ memberplanhandler
------------+ pumpitproj
------------+ site_media

我在 /srv/pumpit/pumpit2/pumpitproj/pumpitproj/settings.py 中的 INSTALLED_APPS 和 ROOT_URLCONF 如下所示:

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Uncomment the next line to enable the admin:
    # 'django.contrib.admin',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
    'pumpitproj.loginhandler',
)

ROOT_URLCONF = 'pumpitproj.urls'

我的 /srv/pumpit/pumpit2/pumpitproj/pumpitproj/urls.py 如下:

from django.conf.urls import patterns, include, url

# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()

urlpatterns = patterns('',
    url(r'^login/', include('pumpitproj.loginhandler.urls')),
)

我的 /srv/pumpit/pumpit2/pumpitproj/loginhandler/urls.py 如下:

from django.conf.urls import patterns, url
from loginhandler import views

urlpatterns = patterns('pumpitproj.loginhandler.views',
    url(r'^$', 'index'),
)

我的 /srv/pumpit/pumpit2/conf/apache/django.wsgi 如下:

import os
import sys

root_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..',$
sys.path.insert(0, os.path.abspath(os.path.join(root_path, 'venv/lib/python2.7/$
sys.path.insert(0, os.path.abspath(os.path.join(root_path, 'pumpit2')))
sys.path.insert(0, os.path.abspath(os.path.join(root_path, 'pumpit2', 'pumpitpr$

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

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

我不确定在这方面可能还需要什么帮助。我确信这是我完全忽略的简单事情。

我花了很多时间寻找答案。我似乎发现很多其他人都有 ImportError 问题,但是我遇到的对其他人有用的答案都没有对我有用。我能得到的任何帮助将不胜感激。

更新:我最终只是删除了 loginhandler 文件夹并重新制作了应用程序并解决了问题。我不知道出了什么问题。

虽然,我显然不知道删除模块/应用程序的正确方法,因为当我尝试制作新模块/应用程序时,它表示已经存在具有该名称的模块。我最终不得不给它起个别的名字。

4

1 回答 1

0

在已安装的应用程序中正确添加您的应用程序名称,此处添加了“pumpitproj.loginhandler”,请将其更改为“loginhandler”并正确设置您的ROOT_URLCONF。

如果您在应用程序之外创建 urls.py,则设置

ROOT_URLCONF = 'urls'

如果您在模块 loginhandler 中创建 urls.py,则设置

ROOT_URLCONF = 'loginhandler.urls'

也改变

 url(r'^login/', include('pumpitproj.loginhandler.urls'))

 url(r'^login/', include('loginhandler.urls'))

我的 /srv/pumpit/pumpit2/pumpitproj/loginhandler/urls.py 如下:

from django.conf.urls import patterns, url
from loginhandler import views

urlpatterns = patterns('loginhandler.views',
    url(r'^$', 'index'),
)

试试这个改变。

于 2013-04-03T05:20:42.797 回答