开发者

How to make if/else statement in Django Html Template?

开发者 https://www.devze.com 2023-04-03 03:16 出处:网络
I want to test for the user\'s browser. If Internet Explorer, then this paragraph. els开发者_运维技巧e this paragraph.

I want to test for the user's browser. If Internet Explorer, then this paragraph. els开发者_运维技巧e this paragraph.

How do I do that in a django html template?

Thanks!


Don't do it using Django. Use conditional comments. Example:

<!--[if IE]>
According to the conditional comment this is Internet Explorer<br />
<![endif]-->
<!--[if gte IE 5]>
According to the conditional comment this is Internet Explorer 5 and up<br />
<![endif]-->
<!--[if gt IE 6]>
According to the conditional comment this is Internet Explorer greater than 6<br />
<![endif]-->


Django templates are rendered on the server side, what actually goes to the browser is the output is the rendered template. So you can't write browser specific code in the template, unless you capture that information from the request, pass it as a variable to the template, when you render it, which then uses that value to produce browser specific code.


Don't. There should almost never be a need for this. If you really really think you need it, use IE's conditional comments.

0

精彩评论

暂无评论...
验证码 换一张
取 消