Django翻译重定向回当前页面



如何重定向回当前页面在我的网站上,我正在实现两种语言,即"en"one_answers"fa"现在它正在工作,但没有像docs.djangoproject.com那样重定向到当前页面,而是将我重定向到主页"localhost:8000/fa/"或/en-

这是代码:用于模板侦听器.py

<li class="dropdown default-dropdown">
<form action="{% url 'selectlanguage' %}" method="POST">{% csrf_token %}
<input name="next" type="hidden" value="{{ redirect_to }}">
<select name="language">
{% get_current_language as LANGUAGE_CODE %}
{% get_available_languages as LANGUAGES %}
{% get_language_info_list for LANGUAGES as languages %}
{% for language in languages %}
<option value="{{ language.code }}"{% if language.code == LANGUAGE_CODE %} selected{% endif %}>
{{ language.name_local }} ({{ language.code }})
</option>
{% endfor %}
</select>
<input type="submit" value="{% trans 'Go' %}">
</form>
</li>

urls.py的代码是:

path('selectlanguage', views.selectlanguage, name='selectlanguage'),

对于views.py是:


def selectlanguage(request):
if request.method == 'POST':  # check post
cur_language = translation.get_language()
lasturl= request.META.get('HTTP_REFERER')
lang = request.POST['language']
translation.activate(lang)
request.session[translation.LANGUAGE_SESSION_KEY]=lang
#return HttpResponse(lang)
return HttpResponseRedirect(lang)

您是否将url包装在i18n_patterns中?

from django.conf.urls.i18n import i18n_patterns
urlpatterns += i18n_patterns(
path('about/', about_views.main, name='about'),
path('news/', include(news_patterns, namespace='news')),
)

相关内容

  • 没有找到相关文章

最新更新