目前在django-cms 2.4.2上。遇到一个特殊的问题,当在DEBUG = True的本地机器上,访问127.0.0.1/将重定向到127.0.0.1/en/。在生产服务器(virtualenvs,apache)上运行时,访问mysite.com/将引发404,但在mysite.com/en/显示主页时显示主页。一直试图找到问题的根源,但似乎无法弄清楚它是否在网址,wsgi或设置中。
urls.py
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin_tools/', include('admin_tools.urls')),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
urlpatterns += i18n_patterns('',
url(r'^accounts/', include('allauth.urls')),
... apps ...
url(r'^', include('cms.urls')),
)
# Static files
urlpatterns += staticfiles_urlpatterns()
if settings.DEBUG:
urlpatterns = patterns('',
(r'^' + settings.MEDIA_URL.lstrip('/'), include('appmedia.urls')),
) + urlpatterns
else:
from django.conf.urls.defaults import *
handler500 = 'error_pages.views.server_error'
handler404 = 'error_pages.views.custom_404'
handler403 = 'error_pages.views.permission_denied'
handler400 = 'error_pages.views.bad_request'
我在设置中添加了django.middleware.locale.LocaleMiddleware
和cms.middleware.language.LanguageCookieMiddleware
。
在我的Apache httpd.conf中:
NameVirtualHost *:27567
# Begin site configuration
<VirtualHost *:27567>
WSGIScriptAlias / /path/to/wsgi.$
ErrorLog "/path/to/log/error_path.log"
</VirtualHost>
尝试记录django可能遇到并且只收到的任何错误:
WARNING 2013-09-18 14:04:34,380 Not Found: /
修改
似乎在Debug = True时,问题就消失了。我已将allowed_hosts设置设为'localhost'
和'domain.com'
。检查了加载的中间件序列,i18n设置为true等但仍然无法确定问题所在。
编辑2:
刚发现问题。它位于错误页面中,错误页面的视图为:
来自django.shortcuts导入渲染
def server_error(request):
return render(request, '500.html')
很可能是django-cms'网址之间的冲突。