开发者

Excluding a Django app from being localized using a middleware

开发者 https://www.devze.com 2023-01-18 23:23 出处:网络
I need to localize a django project, but keep one of the applications (the blog) English only. I wrote this middleware in order to achieve this:

I need to localize a django project, but keep one of the applications (the blog) English only.

I wrote this middleware in order to achieve this:

from django.conf import settings
from django.core.urlresolvers import r开发者_如何转开发esolve

class DelocalizeMiddleware:
    def process_request(self, request):
        current_app_name = __name__.split('.')[-2]
        match = resolve(request.path)
        if match.app_name == current_app_name:
            request.LANGUAGE_CODE = settings.LANGUAGE_CODE

Problem is, it assumes the middleware lies directly in the application module (e.g. blog/middleware.py) for retrieving the app name. Other projects might have the middleware in blog/middleware/delocalize.py or something else altogether.

What's the best way to retrieve the name of the currently running app?


You can use Django's resolve function to get the current app name.

https://docs.djangoproject.com/en/dev/topics/http/urls/#resolve

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号