重新加载当前页面时,Django 不会刷新我的请求对象
我有一个 Django 网站,我希望它可以用不同的语言查看。直到今天早上一切都工作正常。这是交易。我进入我的“关于我们”页面,它是英文的。在它下面有一个更改语言按钮,当我按下它时,所有内容都会“神奇地”翻译成保加利亚语,就像我想要的那样。另一方面,我有一个 JS 菜单,用户可以从中浏览产品。我点击“T 恤”,然后在之前按下的包含不同类别的子菜单下方打开一个子菜单 - 男士、女士、儿童。该链接引导我进入一个页面,其中列出了我所要求的确切衣服。但是...当我尝试更改语言时,什么也没有发生。我转到“关于”页面,从那里更改语言,返回到衣服目录,语言被更改......
我不会粘贴一些代码。
这是我的更改按钮代码:
function changeLanguage() {
if (getCookie('language') == 'EN') {
setCookie("language", 'BG');
} else {
setCookie("language", 'EN');
}
window.location.reload();
}
我的“关于我们”页面:
@base
def aboutUs(request):
return """<b>%s</b>""" % getTranslation("About Us Text", request.COOKIES['language'])
@base 方法:
def base(myfunc):
def inner_func(*args, **kwargs):
try:
args[0].COOKIES['language']
except:
args[0].COOKIES['language'] = 'BG'
# raise Exception(request)
# if I am in the AboutUs page
# and I click on the language change button
# the cookie value in the request object changes
# if however I am in the displayClothes page
# the value stays the same
# some code I removed
contents = myfunc(*args, **kwargs)
return render_to_response('index.html', {'title': title, 'categoriesByCollection': categoriesByCollection.iteritems(), 'keys': enumerate(keys), 'values': enumerate(values), 'contents': contents, 'btnHome':getTranslation("Home Button", args[0].COOKIES['language']), 'btnProducts':getTranslation("Products Button", args[0].COOKIES['language']), 'btnOrders':getTranslation("Orders Button", args[0].COOKIES['language']), 'btnAboutUs':getTranslation("About Us Button", args[0].COOKIES['language']), 'btnContacts':getTranslation("Contact Us Button", args[0].COOKIES['language']), 'btnChangeLanguage':getTranslation("Button Change Language", args[0].COOKIES['language'])})
return inner_func
和目录页面:
@base
def displayClothes(request, category, collection, page):
clothesToDisplay = getClothesFromCollectionAndCategory(request, category, collection)
contents = ""
# some code I removed
return """%s""" % (contents)
让我解释一下,您不必对我发布的大量代码感到震惊。你不必理解它,甚至不需要看全部。我发布它只是为了以防万一,因为我真的无法理解该错误的起源。
现在这就是我缩小问题范围的方法。每当我想知道请求对象中的内容时,我都会使用“引发异常(请求)”进行调试。当我将其放入 aboutUs 方法中时,每次按下语言按钮时,语言 cookie 值都会发生变化。但当我在 displayClothes 方法中时则不然。那里的语言保持不变。我还尝试将异常行放在 @base 方法的开头。事实证明那里的情况一模一样。当我在“关于我们”页面中单击按钮时,请求对象中的语言会发生变化,但当我在目录页面中按下按钮时,它保持不变。
这就是我能找到的全部内容,我不知道 Django 如何以及以何种方式区分我的页面。
PS 我认为 JavaScript 工作得很好,我已经通过多种方式对其进行了测试。
谢谢,我希望你们中的一些人能够阅读这篇巨大的文章,并毫不犹豫地索取更多代码摘录。
I have a Django web site which I want ot be viewable in different languages. Until this morning everything was working fine. Here is the deal. I go to my say About Us page and it is in English. Below it there is the change language button and when I press it everything "magically" translates to Bulgarian just the way I want it. On the other hand I have a JS menu from which the user is able to browse through the products. I click on 'T-Shirt' then a sub-menu opens bellow the previously pressed containing different categories - Men, Women, Children. The link guides me to a page where the exact clothes I have requested are listed. BUT... When I try to change the language THEN, nothing happens. I go to the Abouts Page, change the language from there, return to the clothes catalog and the language is changed...
I will no paste some code.
This is my change button code:
function changeLanguage() {
if (getCookie('language') == 'EN') {
setCookie("language", 'BG');
} else {
setCookie("language", 'EN');
}
window.location.reload();
}
My About Us page:
@base
def aboutUs(request):
return """<b>%s</b>""" % getTranslation("About Us Text", request.COOKIES['language'])
The @base method:
def base(myfunc):
def inner_func(*args, **kwargs):
try:
args[0].COOKIES['language']
except:
args[0].COOKIES['language'] = 'BG'
# raise Exception(request)
# if I am in the AboutUs page
# and I click on the language change button
# the cookie value in the request object changes
# if however I am in the displayClothes page
# the value stays the same
# some code I removed
contents = myfunc(*args, **kwargs)
return render_to_response('index.html', {'title': title, 'categoriesByCollection': categoriesByCollection.iteritems(), 'keys': enumerate(keys), 'values': enumerate(values), 'contents': contents, 'btnHome':getTranslation("Home Button", args[0].COOKIES['language']), 'btnProducts':getTranslation("Products Button", args[0].COOKIES['language']), 'btnOrders':getTranslation("Orders Button", args[0].COOKIES['language']), 'btnAboutUs':getTranslation("About Us Button", args[0].COOKIES['language']), 'btnContacts':getTranslation("Contact Us Button", args[0].COOKIES['language']), 'btnChangeLanguage':getTranslation("Button Change Language", args[0].COOKIES['language'])})
return inner_func
And the catalog page:
@base
def displayClothes(request, category, collection, page):
clothesToDisplay = getClothesFromCollectionAndCategory(request, category, collection)
contents = ""
# some code I removed
return """%s""" % (contents)
Let me explain that you needn't be alarmed by the large quantities of code I have posted. You don't have to understand it or even look at all of it. I've published it just in case because I really can't understand the origins of the bug.
Now this is how I have narrowed the problem. I am debuging with "raise Exception(request)" every time I want to know what's inside my request object. When I place this in my aboutUs method, the language cookie value changes every time I press the language button. But NOT when I am in the displayClothes method. There the language stays the same. Also I tried putting the exception line in the beginning of the @base method. It turns out the situation there is exactly the same. When I am in my About Us page and click on the button, the language in my request object changes, but when I press the button while in the catalog page it remains unchanged.
That is all I could find, and I have no idea as to how Django distinguishes my pages and in what way.
P.S. The JavaScript I think works perfectly, I have tested it in multiple ways.
Thank you, I hope some of you will read this enormous post, and don't hesitate to ask for more code excerpts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码太多,难以阅读。你确实需要努力削减它。
如果您确定错误位于
displayClothes
中的某个位置,那么我会注释掉一些位,直到您不再收到错误为止。但似乎没有任何东西可以改变该视图中的cookie,所以我不知道你会取得多大的成功。另外,请确保您检查的是AboutUs
请求中的实际内容,而不仅仅是您认为的内容。旁注:您将 HTML 直接硬编码到视图中。我很确定您并不真的想这样做 - Django 拥有模板是有原因的。
That's far too much code to read through. You really need to make an effort to trim it down.
If you're sure that the error is somewhere in
displayClothes
, then I would comment bits out until you no longer get the error. But there doesn't appear to be anything there which changes the cookies in that view, so I don't know how successful you'll be. Also make sure that you're checking against what's actually in the request forAboutUs
, not just what you think it is.A side note: you're hard coding HTML directly into your views. I'm pretty sure that you don't really want to do that - Django has templates for a reason.