admin管理员组文章数量:1308623
I am trying to build a web site in both English and Bulgarian using the Django framework. My idea is the user should click on a button, the page will reload and the language will be changed. This is how I am trying to do it:
In my html I hava a the button tag <button id='btn' onclick="changeLanguage();" type="button"> ... </button>
An excerpt from cookies.js:
function changeLanguage() {
if (getCookie('language') == 'EN') {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'BG');
} else {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'EN');
}
}
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
var sCookie = sName + "=" + encodeURIComponent(sValue);
if (oExpires) {
sCookie += "; expires=" + oExpires.toGMTString();
}
if (sPath) {
sCookie += "; path=" + sPath;
}
if (sDomain) {
sCookie += "; domain=" + sDomain;
}
if (bSecure) {
sCookie += "; secure";
}
document.cookie = sCookie;
}
And in my views.py file this is the situation
@base
def index(request):
if request.session['language'] == 'EN':
return """<b>%s</b>""" % "Home"
else request.session['language'] == 'BG':
return """<b>%s</b>""" % "Начало"
So I know that my JS changes the value of the language cookie but I think Django doesn't get that. On the other hand when I set and get the cookie in my Python code again the cookie is set. My question is whether there is a way to make JS and Django work together - JavaScript sets the cookie value and Python only reads it when asked and takes adequate actions?
Thank you.
I am trying to build a web site in both English and Bulgarian using the Django framework. My idea is the user should click on a button, the page will reload and the language will be changed. This is how I am trying to do it:
In my html I hava a the button tag <button id='btn' onclick="changeLanguage();" type="button"> ... </button>
An excerpt from cookies.js:
function changeLanguage() {
if (getCookie('language') == 'EN') {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'BG');
} else {
document.getElementById('btn').innerHTML = getCookie('language');
setCookie("language", 'EN');
}
}
function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) {
var sCookie = sName + "=" + encodeURIComponent(sValue);
if (oExpires) {
sCookie += "; expires=" + oExpires.toGMTString();
}
if (sPath) {
sCookie += "; path=" + sPath;
}
if (sDomain) {
sCookie += "; domain=" + sDomain;
}
if (bSecure) {
sCookie += "; secure";
}
document.cookie = sCookie;
}
And in my views.py file this is the situation
@base
def index(request):
if request.session['language'] == 'EN':
return """<b>%s</b>""" % "Home"
else request.session['language'] == 'BG':
return """<b>%s</b>""" % "Начало"
So I know that my JS changes the value of the language cookie but I think Django doesn't get that. On the other hand when I set and get the cookie in my Python code again the cookie is set. My question is whether there is a way to make JS and Django work together - JavaScript sets the cookie value and Python only reads it when asked and takes adequate actions?
Thank you.
Share Improve this question asked May 28, 2010 at 17:53 Boris RusevBoris Rusev 6381 gold badge10 silver badges17 bronze badges1 Answer
Reset to default 10The session is not the same as a cookie.
Sessions are an internal Django database table, the key to which is stored in a cookie. However the rest of the data apart from the key is stored in the database.
If you want to access an actual cookie that's been set by the client, you need to use the request.COOKIES
dictionary:
if request.COOKIES['language'] == 'EN':
return """<b>%s</b>""" % "Home"
本文标签: pythonJavaScript cookie value can39t be retrieved in DjangoStack Overflow
版权声明:本文标题:python - JavaScript cookie value can't be retrieved in Django - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741861839a2401670.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论