Django应用程序通过runserver运行,但错误通过mod_wsgi(NoReverseMatch错误)

时间:2013-08-01 13:18:46

标签: python django mod-wsgi

我有一个有趣的情况:

我有一个django应用程序如下:

[Tintin@BlisteringBarnacles DjangoProj]$ ls -l
total 20
drwxrwxr-x 4 Tintin Tintin 4096 Jul 31 18:50 app_1
-rw-r--r-- 1 Tintin Tintin  335 Aug  1 12:55 django.wsgi
-rw-r--r-- 1 Tintin Tintin  252 Jul 31 18:48 manage.py
drwxrwxr-x 2 Tintin Tintin 4096 Jul 31 18:50 Main_app
-rw-rw-r-- 1 Tintin Tintin  272 Aug  1 10:42 uwsgi.ini
[Tintin@BlisteringBarnacles DjangoProj]$

[Tintin@BlisteringBarnacles DjangoProj]$ ls -l
total 32
-rw-r--r-- 1 Tintin Tintin    0 Jul 31 18:48 __init__.py
-rw-r--r-- 1 Tintin Tintin  149 Jul 31 18:49 __init__.pyc
-rw-r--r-- 1 Tintin Tintin 6178 Aug  1 11:45 settings.py
-rw-r--r-- 1 Tintin Tintin 3428 Jul 31 18:49 settings.pyc
-rw-r--r-- 1 Tintin Tintin  985 Aug  1 12:49 urls.py
-rw-r--r-- 1 Tintin Tintin  408 Jul 31 18:48 urls.pyc
-rw-r--r-- 1 Tintin Tintin 1428 Jul 31 18:48 wsgi.py
-rw-r--r-- 1 Tintin Tintin 1049 Jul 31 18:50 wsgi.pyc
[Tintin@BlisteringBarnacles DjangoProj]$

[Tintin@BlisteringBarnacles DjangoProj]$ cat 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 to app_1/
                  url(r'^app_1/', include('app_1.urls', namespace='app_1_namespace'))
)
[Tintin@BlisteringBarnacles DjangoProj]$

[Tintin@BlisteringBarnacles app-1]$ ls -l
total 40
-rw-r--r-- 1 Tintin Tintin    0 Jul 31 18:48 __init__.py
-rw-r--r-- 1 Tintin Tintin  156 Jul 31 18:50 __init__.pyc
-rw-r--r-- 1 Tintin Tintin   57 Jul 31 18:48 models.py
-rw-r--r-- 1 Tintin Tintin  210 Jul 31 18:50 models.pyc
drwxrwxr-x 3 Tintin Tintin 4096 Jul 31 18:48 static
drwxrwxr-x 3 Tintin Tintin 4096 Jul 31 18:48 templates
-rw-r--r-- 1 Tintin Tintin  383 Jul 31 18:48 tests.py
-rw-r--r-- 1 Tintin Tintin  775 Jul 31 18:48 urls.py
-rw-r--r-- 1 Tintin Tintin  717 Jul 31 18:48 urls.pyc
-rw-r--r-- 1 Tintin Tintin 2961 Jul 31 18:48 views.py
-rw-r--r-- 1 Tintin Tintin 3150 Jul 31 18:48 views.pyc
[Tintin@BlisteringBarnacles app-1]$

urlpatterns = patterns('',
                       # Match the index.html page.
                       url(r'^$', views.index, name='home_page'),
                       # Show the appropriate environment.
                       url(r'^show_environment/(?P<environment_name>\w+)/$',
                           views.show_environment,
                        name="show_environment_name"),
                       # About page.
                       url(r'^show_about/$', views.show_about, name='show_about'),
                       # Show archive data.
                       url (r'^request_archive_data/$', views.show_request_archive_page, name='request_archive_data')
                       )
[Tintin@BlisteringBarnacles app-1]$

所以这是有趣的部分......当我通过runserver运行它时,应用程序工作很棒。但当我在Apache后部署它时,我在 django_logs.log

中看到以下内容
ERROR 2013-08-01 08:55:30,066 base 18235 140176797026304 Line# 215: Internal Server Error: /app_1/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/home/Tintin/Programs/Django/Main_app/app_1/views.py", line 17, in index
    return HttpResponse(template.render (context))
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 140, in render
    return self._render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/loader_tags.py", line 123, in render
    return compiled_parent._render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 134, in _render
    return self.nodelist.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/base.py", line 823, in render
    bit = self.render_node(node, context)
  File "/usr/local/lib/python2.7/site-packages/django/template/debug.py", line 74, in render_node
    return node.render(context)
  File "/usr/local/lib/python2.7/site-packages/django/template/defaulttags.py", line 424, in render
    raise e
NoReverseMatch: u"'app_1_namespace" is not a registered namespace

然后我修改了我的Project以不使用命名空间并修改模板。我现在观察到当渲染函数尝试在home_page的第一个url上执行反向URL查找时,我得到了同样的错误。

以下是我收到错误的模板:

<a class="brand" href="{% url 'app_1_namespace:home_page' %}">Home</a>

我正在使用django 1.4

[Tintin@BlisteringBarnacles Main_app]$ python
Python 2.7.4 (default, Apr 23 2013, 07:53:20)
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 4, 3, 'final', 0)
>>> exit()
[Tintin@BlisteringBarnacles Main_app]$

[Tintin@BlisteringBarnacles Main_app]$ cat django.wsgi
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'Main_app.settings'
os.environ['PYTHON_EGG_CACHE'] = '/home/Tintin/.python-eggs'

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

path = '/home/Tintin/Programs/Django/Main_app'
if path not in sys.path:
    sys.path.append(path)
[Tintin@BlisteringBarnacles Main_app]$

我通读了Django应用程序并且在网上搜索没有产生很多结果。也许我错过了一些非常简单的事情。我很感激对此的投入。

...问候

1 个答案:

答案 0 :(得分:0)

亚当 - 我终于通过说明未来的加载网址来实现它。尽管触摸了django.wsgi,但最初我仍然遇到错误。但apache httpd的停止和启动似乎已经修复了它。从不同的机器测试它。非常感谢Sir !!