在阅读了 Django 文档后,意识到 Django 开发人员似乎也在执行他们自己的编码标准(这里我特别考虑了源代码中的这条评论:
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.
) 我决定采用in this post 概述的解决方案,我将(几乎)逐字粘贴在下面(因此是非语义表格布局):
<div id="languages">
<table >
<tr>
{% get_available_languages as languages %}
{% for key, item in languages %}
<td>
<form action="/i18n/setlang/" method="post">
{% csrf_token %}
<input type="hidden" name="language" value="{{key}}"/>
<input id="lang_select_{{key}}" type = "image" src="/media/img/{{ key }}.gif" alt="{{ item }}"/>
</form>
</td>
{% endfor %}
</tr>
</table>
</div>
我还提出了我自己的 JS 解决方案,它更灵活,也更复杂,所以它并不值得(虽然它提供了链接:但因为他们需要一个 onClick 属性,或道德等价物,他们对 SEO 目的毫无用处)。
当然,抽出代码并消除愚蠢的 GET 限制也是一种选择 - 尽管维护性更高。
P.S:那些发现我的评论高于对抗性的人(而且,毫无疑问,我的任务被误导了)应该考虑需要 POST 来切换语言的立场如何在 SE 遍历中飞行,同时强制跳过这种箍尊重既定的设计惯例,即允许用户通过单击标志图标来切换语言。