本文整理匯總了Python中django.conf.settings.LANGUAGE_COOKIE_NAME屬性的典型用法代碼示例。如果您正苦於以下問題:Python settings.LANGUAGE_COOKIE_NAME屬性的具體用法?Python settings.LANGUAGE_COOKIE_NAME怎麽用?Python settings.LANGUAGE_COOKIE_NAME使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類django.conf.settings
的用法示例。
在下文中一共展示了settings.LANGUAGE_COOKIE_NAME屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: set_user_language
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def set_user_language(request):
next = request.REQUEST.get('next')
if not is_safe_url(url=next, host=request.get_host()):
next = request.META.get('HTTP_REFERER')
if not is_safe_url(url=next, host=request.get_host()):
next = '/'
response = HttpResponseRedirect(next)
if request.method == 'POST':
lang_code = request.POST.get('language', None)
if 'ref' not in next:
ref = urlparse(request.POST.get('referrer', next))
response = HttpResponseRedirect('?ref='.join([next, ref.path]))
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
user = request.user
if user.is_authenticated():
user_profile = user.profile
user_profile.language = lang_code
user_profile.save()
return response
示例2: set_language
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def set_language(request):
"""
Redirect to a given url while setting the chosen language in the
session or cookie. The url and the language code need to be
specified in the request parameters.
Since this view changes how the user will see the rest of the site, it must
only be accessed as a POST request. If called as a GET request, it will
redirect to the page in the request (the 'next' parameter) without changing
any state.
"""
next = request.REQUEST.get('next')
if not is_safe_url(url=next, host=request.get_host()):
next = request.META.get('HTTP_REFERER')
if not is_safe_url(url=next, host=request.get_host()):
next = '/'
response = http.HttpResponseRedirect(next)
if request.method == 'POST':
lang_code = request.POST.get('language', None)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code)
return response
示例3: process_request
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def process_request(self, request):
lang_cookie = request.session.get(settings.LANGUAGE_COOKIE_NAME)
if not lang_cookie:
authenticated = request.user.is_authenticated
if authenticated:
try:
profile = get_user_profile(user=request.user)
except (ObjectDoesNotExist, SiteProfileNotAvailable):
profile = False
if profile:
try:
lang = getattr(profile, userena_settings.USERENA_LANGUAGE_FIELD)
translation.activate(lang)
request.LANGUAGE_CODE = translation.get_language()
except AttributeError:
pass
示例4: test_setlang
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def test_setlang(self):
"""
The set_language view can be used to change the session language.
The user is redirected to the 'next' argument if provided.
"""
lang_code = self._get_inactive_language_code()
post_data = {'language': lang_code, 'next': '/'}
response = self.client.post('/i18n/setlang/', post_data, HTTP_REFERER='/i_should_not_be_used/')
self.assertRedirects(response, '/')
self.assertEqual(self.client.session[LANGUAGE_SESSION_KEY], lang_code)
# The language is set in a cookie.
language_cookie = self.client.cookies[settings.LANGUAGE_COOKIE_NAME]
self.assertEqual(language_cookie.value, lang_code)
self.assertEqual(language_cookie['domain'], '')
self.assertEqual(language_cookie['path'], '/')
self.assertEqual(language_cookie['max-age'], '')
示例5: test_setlang_cookie
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def test_setlang_cookie(self):
# we force saving language to a cookie rather than a session
# by excluding session middleware and those which do require it
test_settings = {
'MIDDLEWARE': ['django.middleware.common.CommonMiddleware'],
'LANGUAGE_COOKIE_NAME': 'mylanguage',
'LANGUAGE_COOKIE_AGE': 3600 * 7 * 2,
'LANGUAGE_COOKIE_DOMAIN': '.example.com',
'LANGUAGE_COOKIE_PATH': '/test/',
}
with self.settings(**test_settings):
post_data = {'language': 'pl', 'next': '/views/'}
response = self.client.post('/i18n/setlang/', data=post_data)
language_cookie = response.cookies.get('mylanguage')
self.assertEqual(language_cookie.value, 'pl')
self.assertEqual(language_cookie['domain'], '.example.com')
self.assertEqual(language_cookie['path'], '/test/')
self.assertEqual(language_cookie['max-age'], 3600 * 7 * 2)
示例6: setlang
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def setlang(request):
"""
Sets a user's language preference and redirects to a given URL or, by default, back to the previous page.
"""
next = request.GET.get('next', None)
if not is_safe_url(url=next, host=request.get_host()):
next = request.META.get('HTTP_REFERER')
if not is_safe_url(url=next, host=request.get_host()):
next = '/'
response = redirect(next)
lang_code = request.GET.get('language', None)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session[LANGUAGE_SESSION_KEY] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code,
max_age=settings.LANGUAGE_COOKIE_AGE,
path=settings.LANGUAGE_COOKIE_PATH,
domain=settings.LANGUAGE_COOKIE_DOMAIN)
return response
示例7: handle
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def handle(self, request, data):
response = shortcuts.redirect(request.build_absolute_uri())
# Language
lang_code = data['language']
if lang_code and translation.check_for_language(lang_code):
if hasattr(request, 'session'):
request.session['django_language'] = lang_code
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code,
expires=_one_year())
# Timezone
request.session['django_timezone'] = pytz.timezone(
data['timezone']).zone
response.set_cookie('django_timezone', data['timezone'],
expires=_one_year())
request.session['horizon_pagesize'] = data['pagesize']
response.set_cookie('horizon_pagesize', data['pagesize'],
expires=_one_year())
with translation.override(lang_code):
messages.success(request,
encoding.force_text(_("Settings saved.")))
return response
示例8: user_profile_save_preferences
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def user_profile_save_preferences(request):
"""Saves the form"""
profile = request.user.userprofile
form = UserProfileEditPreferencesForm(data=request.POST, instance=profile)
response_dict = {'form': form}
response = HttpResponseRedirect("/profile/edit/preferences/")
if form.is_valid():
form.save()
# Activate the chosen language
from django.utils.translation import check_for_language, activate
lang = form.cleaned_data['language']
if lang and check_for_language(lang):
if hasattr(request, 'session'):
request.session['django_language'] = lang
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang)
activate(lang)
else:
return render(request, "user/profile/edit/preferences.html", response_dict)
messages.success(request, _("Form saved. Thank you!"))
return response
示例9: set_language
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def set_language(request, lang):
from django.utils.translation import check_for_language, activate
next = request.GET.get('next', None)
if not next:
next = request.META.get('HTTP_REFERER', None)
if not next:
next = '/'
response = HttpResponseRedirect(next)
if lang and check_for_language(lang):
if hasattr(request, 'session'):
request.session['django_language'] = lang
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang)
activate(lang)
if request.user.is_authenticated():
profile = request.user.userprofile
profile.language = lang
profile.save(keep_deleted=True)
return response
示例10: set_language
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def set_language(request):
"""
Redirect to a given url while setting the chosen language in the
session or cookie. The url and the language code need to be
specified in the request parameters.
Since this view changes how the user will see the rest of the site, it must
only be accessed as a POST request. If called as a GET request, it will
redirect to the page in the request (the 'next' parameter) without changing
any state.
"""
next = request.POST.get('next', request.GET.get('next'))
if not is_safe_url(url=next, host=request.get_host()):
next = request.META.get('HTTP_REFERER')
if not is_safe_url(url=next, host=request.get_host()):
next = '/'
response = http.HttpResponseRedirect(next)
if request.method == 'POST':
lang_code = request.POST.get('language', None)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session[LANGUAGE_SESSION_KEY] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code,
max_age=settings.LANGUAGE_COOKIE_AGE,
path=settings.LANGUAGE_COOKIE_PATH,
domain=settings.LANGUAGE_COOKIE_DOMAIN)
return response
示例11: set_language
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def set_language(request):
"""
Redirect to a given URL while setting the chosen language in the session or
cookie. The URL and the language code need to be specified in the request
parameters.
Since this view changes how the user will see the rest of the site, it must
only be accessed as a POST request. If called as a GET request, it will
redirect to the page in the request (the 'next' parameter) without changing
any state.
"""
next = request.POST.get('next', request.GET.get('next'))
if ((next or not request.is_ajax()) and
not is_safe_url(url=next, allowed_hosts={request.get_host()}, require_https=request.is_secure())):
next = request.META.get('HTTP_REFERER')
if next:
next = unquote(next) # HTTP_REFERER may be encoded.
if not is_safe_url(url=next, allowed_hosts={request.get_host()}, require_https=request.is_secure()):
next = '/'
response = HttpResponseRedirect(next) if next else HttpResponse(status=204)
if request.method == 'POST':
lang_code = request.POST.get(LANGUAGE_QUERY_PARAMETER)
if lang_code and check_for_language(lang_code):
if next:
next_trans = translate_url(next, lang_code)
if next_trans != next:
response = HttpResponseRedirect(next_trans)
if hasattr(request, 'session'):
request.session[LANGUAGE_SESSION_KEY] = lang_code
else:
response.set_cookie(
settings.LANGUAGE_COOKIE_NAME, lang_code,
max_age=settings.LANGUAGE_COOKIE_AGE,
path=settings.LANGUAGE_COOKIE_PATH,
domain=settings.LANGUAGE_COOKIE_DOMAIN,
)
return response
示例12: check_page
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def check_page(request, dissemin_base_client, validator_tools):
"""
Checks status of page and checks html.
"""
def checker(status, *args, **kwargs):
vt = validator_tools
vt.client = kwargs.pop('client', dissemin_base_client)
vt.client.cookies.load({settings.LANGUAGE_COOKIE_NAME : request.param})
vt.check_page(status, *args, **kwargs)
return checker
示例13: check_url
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def check_url(request, validator_tools):
"""
Checks status and html of a URL
"""
def checker(status, url):
vt = validator_tools
vt.client.cookies.load({settings.LANGUAGE_COOKIE_NAME : request.param})
vt.check_url(status, url)
return checker
示例14: test_cookie
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def test_cookie(self) -> None:
languages = [('en', 'Sign up'),
('de', 'Registrieren'),
('sr', 'Упишите се'),
('zh-hans', '注冊'),
]
for lang, word in languages:
# Applying str function to LANGUAGE_COOKIE_NAME to convert unicode
# into an ascii otherwise SimpleCookie will raise an exception
self.client.cookies = SimpleCookie({str(settings.LANGUAGE_COOKIE_NAME): lang})
response = self.fetch('get', '/integrations/', 200)
self.assert_in_response(word, response)
示例15: get_lang_from_cookie
# 需要導入模塊: from django.conf import settings [as 別名]
# 或者: from django.conf.settings import LANGUAGE_COOKIE_NAME [as 別名]
def get_lang_from_cookie(request, supported):
"""See if the user's browser sent a cookie with a preferred language."""
from django.conf import settings
lang_code = request.COOKIES.get(settings.LANGUAGE_COOKIE_NAME)
if lang_code and lang_code in supported:
return lang_code
return None