我想将django语言选择实现为一系列按钮,而不是基于下拉选择的表单。我尝试通过向按钮添加formaction
,value
和{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
<form action="{% url 'set_language' %}" method="post">{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<select name="language">
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected="selected"{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>
属性来尝试执行此操作,但它无法正常工作。
将以下输入表单实现为一系列按钮的正确方法是什么?谢谢!
来自Django国际化文档:
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<button type="submit" title="{{ language.code }}" formaction="{% url 'set_language' %}" value="{{ language.code }}">
{{ language.name_local }} ({{ language.code }})
</button>
{% endfor %}
我尝试使用按钮:
{{1}}
答案 0 :(得分:3)
以下是使用提交按钮创建每种语言作为其自己的隐藏表单的方法,该按钮可以是styled with css。
如果您希望以不同方式设置活动语言的样式,可以使用django
if语句更改css(如果它是当前语言)。
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<form action="{% url 'set_language' %}" method="post" id="form_{{ language.code }}" style="display:inline!important;">
{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}" />
<input name="language" type="hidden" value="{{ language.code }}" />
</form>
<button class="lang-button" type="submit" form="form_{{ language.code }}" value="Submit">[{{ language.code }}]</button>
{% endfor %}