Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django locale enabled app always returns 404 when debug is off

I've been developing an app that uses i18n/locale as url prefix in the root/landing page. I tried to deploy my application earlier for testing purposes with DEBUG=True and everything was working perfectly. If attempting to access domain.com it would redirect to domain.com/en/

Now, since I disabled DEBUG=False, it won't redirect to /en/, instead it would show 404 error.

I saw this question, which is close to my scenario

Django 1.4 LocaleMiddleware not working with Apache, but works with runserver

but, for my case, I already have the 404 page setup and its handlers and 404.html file.

Below is my code

settings.py

LANGUAGE_CODE = 'en-us'
ADMIN_LANGUAGE_CODE = LANGUAGE_CODE

gettext = lambda s: s

LANGUAGES = (
    ('ar', gettext('Arabic')),
    ('en', gettext('English')),
)

USE_I18N = True

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'apps.accounts.middleware.AccountSocialAuthExceptionMiddleware',
    'apps.core.middleware.DefaultSiteMiddleware',
    #    'debug_toolbar.middleware.DebugToolbarMiddleware',
    # Uncomment the next line for simple clickjacking protection:
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

TEMPLATE_CONTEXT_PROCESSORS = (

    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.request",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
    "django.core.context_processors.tz",
    "django.contrib.messages.context_processors.messages",
    # custom context processors
    "snowflake.apps.core.context_processors.project_name",
    "snowflake.apps.core.context_processors.sites",


)

and here is my URLs

from django.conf import settings
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib.staticfiles.urls import staticfiles_urlpatterns


urlpatterns = patterns('',
                       url(r'^admin/', include('apps.mod.urls')),
                       url(r'^redirect/login/$', 'apps.core.views.redirect_to_accounts', name='login-redirect'),
)

urlpatterns += i18n_patterns('',

                             url(r'^$', 'apps.publication.views.index', name='home'),

                             url(r'^publication(s)?/', include('apps.publication.urls')),

                             url(r'^comment(s)?/', include('apps.comment.urls')),

                             url(r'^rating(s)?/', include('apps.rating.urls')),

                             url(r'^tag(s)?/', include('apps.taggable.urls')),

                             url(r'^search/', include('apps.search.urls')),

                             url(r'^guard(s)?/', include('apps.guard.urls')),
)

handler404 = 'apps.core.views.handler404'

P.S am using django 1.5

like image 260
Mo J. Mughrabi Avatar asked May 15 '13 09:05

Mo J. Mughrabi


1 Answers

Your view apps.core.view.handler404 returns a HttpResponse object... but its HTTP status code is 200.

response = render_to_response("...")
response.status_code = 404
return response

should solve your problem (have had it before).

like image 193
Steve K Avatar answered Oct 19 '22 14:10

Steve K