I'm using Django's LocaleMiddleware
to internationalize a part of the website I'm working on.
Here is my project's urls.py
:
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
urlpatterns = patterns('',
url(r'^api/stuff/(?P<stuff_id>)\d+/$', ApiStuff.as_view()),
)
urlpatterns += i18n_patterns('',
url(r'^stuff/', DoStuff.as_view()),
)
The problem is, when ApiStuff.as_view()
returns a 404 response (other error codes behave as expected), the LocaleMiddleware
operates the request to make it redirect to /en/api/stuff/<stuff_id>
, even though the /api
namespace is clearly not in the i18n_patterns
(at the end, it generates a 404 error too, but the content of my original response is lost).
Here is the code of ApiStuff
:
import django.http
from django.views.generic import View
from project.stuff.models import Stuff
class ApiStuff(View):
@staticmethod
def get(request, *args, **kwargs):
stuff_id = kwargs['stuff_id']
try:
stuff = Stuff.objects.get(pk=stuff_id)
except Stuff.DoesNotExist:
return response({"error": "stuff not found"}, 404)
result = stuff.serialize()
return response(result)
def response(data, status=200):
data = json.dumps(data)
return django.http.HttpResponse(data, status=status, content_type='application/json')
I'm using django 1.6.10 (I know, it's late, but I can't update the version right now).
Am I missing something?