2

我有一个有趣的情况:

我有一个 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

然后我修改了我的项目以不使用命名空间并修改了模板。我现在观察到,当渲染函数尝试对 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 应用程序并在网上搜索并没有产生很多结果。也许我错过了一些非常简单的东西。我将不胜感激对此的意见。

问候...

4

1 回答 1

0

亚当 - 我终于通过说从未来加载网址来让它工作。最初,尽管触摸了 django.wsgi,但我一直收到错误消息。但是 apache httpd 的停止和启动似乎已经修复了它。从不同的机器上测试它。非常感谢先生!

于 2013-08-01T14:50:24.987 回答