简体   繁体   中英

Get the subdomain in a Django view? using django-hosts library

I'm making a project in django. The aplication have to show diferent images and languages depending on the subdomain. For example: www.mysite.com will be the default page in english, but if the subdomain is mx.mysite.com the language must be spanish.

Whith django-hosts I can make that each subdomain redirect to a diferent django-app and I works well. The problem is that I want to make only one app to all diferent subdomains, only changing the language.

I think that is possible get the subdomain in a view and render the template with the language depending on the subdomain. But I don't know how to do it, please help.

THIS IS MY DJANGO HOSTS.PY where the host 'www' and 'mx' redirect to the same app 'mysite' but must be in diferent languages.

from django.conf import settings
from django_hosts import patterns, host

host_patterns = patterns('',
    host(r'www', 'mysite.urls', name='www'),
    host(r'help', 'help.urls', name='help'),
    host(r'mx', 'mysite.urls', name='mx'),
)

General solution

Here's a general solution, which works with any supported language code and without the django-hosts package.

Write a custom middleware, which checks the subdomain, and then sets the session variable LANGUAGE_SESSION_KEY (or the cookie defined by LANGUAGE_COOKIE_NAME in settings.py. The default name is django_language.) with the language from the subdomain.

The custom middleware: (you can save it as middleware.py in the same folder as settings.py)

from django.conf import settings
from django.utils.translation import LANGUAGE_SESSION_KEY


class SetLanguageFromSubdomain:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        host = request.get_host()

        # Parse language code from the host
        lang = host.split('.')[0]

        if lang == 'www':
           lang = 'en'

        # If requested language is in the languages supported by django,
        # language_session_key session variable is populated with the
        # supported language code.
        for django_language in settings.LANGUAGES:
            if django_language[0] == lang or django_language[0].endswith('-%s' % lang):
                request.session[LANGUAGE_SESSION_KEY] = django_language[0]
                break

        response = self.get_response(request)
        return response

Install this middleware between session and locale middlewares in settings.py

    'django.contrib.sessions.middleware.SessionMiddleware',
    #...
    # put the middleware here (change projectname to your project's name)
    'projectname.middleware.SetLanguageFromSubdomain',
    #...
    'django.middleware.locale.LocaleMiddleware',

More information about how django discovers language preference: https://docs.djangoproject.com/en/2.2/topics/i18n/translation/#how-django-discovers-language-preference

Languages supported by django: https://github.com/django/django/blob/master/django/conf/global_settings.py

More information about how to make a Django project translatable: https://docs.djangoproject.com/en/2.2/topics/i18n/translation/

More information about writing a custom middleware: https://docs.djangoproject.com/en/2.2/topics/http/middleware/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM