How to change language by clicking on link (using GET not POST) (set_language redirect)

瀏覽次數:4,732 次
跳到第一則未讀訊息

Simon Tite

未讀,
2008年4月14日 晚上8:03:562008/4/14
收件者:Django users
The documentation on how the user can change the language of a page/
site (from: http://www.djangoproject.com/documentation/i18n/), in the
section "The set_language redirect view", gives the following code:

<code>
<form action="/i18n/setlang/" method="post">
<input name="next" type="hidden" value="/next/page/" />
<select name="language">
{% for lang in LANGUAGES %}
<option value="{{ lang.0 }}">{{ lang.1 }}</option>
{% endfor %}
</select>
<input type="submit" value="Go" />
</form>
</code>

This of course means the user has to click on the language required,
then click the submit button. The result is put in POST data, and then
the redirect to " /i18n/setlang/" does its magic, then returns you
back to the page you came from, with the new LANGUAGE_CODE set up
correctly and, (here's the magic), the code is also automatically
stored in the user's session and cookie data, providing useful
persistence.

However, instead of clicking on submit, I would prefer the user to
click on just a regular link, so I nead to write something like..

<code>
<a href="/i18n/setlang?lang=fr&name=next>Click here for French</a>
</code>

which means it will use GET data.

The documentation says that redirect to set_language uses POST data,
so I guess my question could be rephrased as: How do I use a simple
link to send POST data to the page I am linking to? And I guess that
maybe this is not a Django-related question at all!

Still, maybe one of you kind people can help me or at least point me
in the right direction.

Thanks, Simon.

Malcolm Tredinnick

未讀,
2008年4月14日 晚上9:47:072008/4/14
收件者:django...@googlegroups.com

On Mon, 2008-04-14 at 17:03 -0700, Simon Tite wrote:
[...]

> However, instead of clicking on submit, I would prefer the user to
> click on just a regular link, so I nead to write something like..
>
> <code>
> <a href="/i18n/setlang?lang=fr&name=next>Click here for French</a>
> </code>
>
> which means it will use GET data.

Changing the language changes the state of your application (it displays
it entirely differently, for a start, and may even change the
information that is presented for defaults, etc). That means it is
inappropriate to be done via an HTTP GET, which should not result in any
significant state change. So that's why Django requires a POST there.

(For bonus points, find the one place in Django's code that doesn't obey
this principle. :-( )

If you want to use GET there, you'll need to write your own view.

> The documentation says that redirect to set_language uses POST data,
> so I guess my question could be rephrased as: How do I use a simple
> link to send POST data to the page I am linking to?

You can't. Href links in HTML are always GET requests. They can
therefore be safely followed by automated processes, etc (cue riotous
laughter from everybody who realises that so many websites don't do
things properly that this isn't true, but the principle is correct).

> And I guess that
> maybe this is not a Django-related question at all!
>
> Still, maybe one of you kind people can help me or at least point me
> in the right direction.

Use Javascript, or write your own view that is like Django's set
language view but allows GET.

Regards,
Malcolm

--
I don't have a solution, but I admire your problem.
http://www.pointy-stick.com/blog/

prz

未讀,
2008年4月15日 凌晨2:54:462008/4/15
收件者:django...@googlegroups.com
Simon Tite wrote:
> The documentation on how the user can change the language of a page/
> site (from: http://www.djangoproject.com/documentation/i18n/), in the
> section "The set_language redirect view", gives the following code:
>

instead of the ponderous ruminations of the the venerous
mal...@pointy-stick.com, here's a trivial
solution

<div id="languages">
<table >
<tr>


{% for lang in LANGUAGES %}

<td>
{% comment %}
<b>Kind of weird', depending on version, Django wants either
a GET or a POST to set the cookie</b>
{% endcomment %}

<form action="/http_frontend/i18n/setlang/" method="post">
<input type="hidden" name="language" value="{{lang.0}}"/>
<input id="lang_{{lang.0}}" type = "image"
src="/media/http_frontend/img/{{ lang.0 }}.gif" alt="{{ lang.1 }}"/>
</form>
</td>
{% endfor %}
</tr>
</table>
</div>

just pass in LANGUAGES from the settings and provide images

And yes, I was baffled to realize that somehow between early 0.96 and
svn versions it seems to have flipped from
GET to POST. POST is more logical of course

-- tony

Simon Tite

未讀,
2008年4月15日 下午6:00:072008/4/15
收件者:Django users


On Apr 15, 8:54 am, prz <p...@net4u.ch> wrote:

> instead of the ponderous ruminations of the the venerous
> malc...@pointy-stick.com, here's a trivial
> solution
>
> <div id="languages">
>           <table >
>             <tr>
>               {% for lang in LANGUAGES %}
>               <td>
>             {% comment %}
>             <b>Kind of weird', depending on version, Django wants either
> a GET or a POST to set the cookie</b>
>             {% endcomment %}
>
>             <form action="/http_frontend/i18n/setlang/" method="post">
>               <input type="hidden" name="language" value="{{lang.0}}"/>
>               <input id="lang_{{lang.0}}" type = "image"
> src="/media/http_frontend/img/{{ lang.0 }}.gif" alt="{{ lang.1 }}"/>
>             </form>
>               </td>
>               {% endfor %}
>             </tr>
>           </table>    
>         </div>
>
> just pass in LANGUAGES from the settings and provide images
>
Thanks to you both for your quick responses. Although I'm afraid the
answer isn't *exactly* the effect I wanted, tony's solution will
probably do the trick (I haven't tried it yet!), and will do fine at
this stage of development. But, awww, I really wanted it to look just
like a regular link, following the CSS rules re colour, hover, etc,
identical to all the other links. Without coding any extra CSS,
specially for this type of "form". One thing though tony, thanks for
putting your example in a loop, i.e one form per language, I was going
to put all four languages into one form, then a submit button. Back to
Programming 101 for me!

-- Simon.

David Seddon

未讀,
2014年2月5日 凌晨4:54:222014/2/5
收件者:django...@googlegroups.com
It might be that you just want a link to the current page in another language.  If you need that, see my comment here: http://stackoverflow.com/questions/11437454/django-templates-get-current-url-in-another-language/21573776#21573776

Sergiy Khohlov

未讀,
2014年2月5日 上午8:41:132014/2/5
收件者:django-users
On Wed, Feb 5, 2014 at 11:54 AM, David Seddon <davids...@gmail.com> wrote:
>> a href="/i18n/setlang?lang=fr&name=next

Take a look at your URL : lang=fr is present and you can use this
information for updating LANGUAGE_CODE variable

Many thanks,

Serge


+380 636150445
skype: skhohlov
回覆所有人
回覆作者
轉寄
0 則新訊息