0

我正在开发一个多语言博客,django 文档中的示例代码对我有用

<form action="/i18n/setlang/" method="post">
        {% csrf_token %}
        <select name="language">
            {% get_language_info_list for LANGUAGES as languages %}
            {% for language in languages %}
            <option value="{{ language.code }}">{{ language.name_local }} ({{ language.code }})</option>
            {% endfor %}
        </select>
        <input type="submit" value="Go" />
    </form>

这种形式让用户选择他们想要的语言。但我实际上想把它放在像这样的链接形式:[FR][EN][VI]我怎样才能在模板中实现它?

4

1 回答 1

4

使用 jquery:

<form action="/i18n/setlang/" method="post" style="display: none" id="change_language_form">
    {% csrf_token %}
    <input type="hidden" value="" name="language" id="language" />
</form>

{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
    <a href="#" lang_code="{{ language.code }}" class="change_language">{{ language.name_local }} ({{ language.code }})</a>
{% endfor %}

<script>
    $('.change_language').click(function(e){
        e.preventDefault();
        $('#language').val($(this).attr('lang_code'));
        $('#change_language_form').submit();
    });
</script>
于 2013-05-05T21:50:13.080 回答