在模板Django中检查变量中字符串的某些部分

时间:2021-12-26 03:18:16

Hi i got the site domain in the template name with the

嗨,我在模板名称中获得了站点域名

{{request.META.HTTP_HOST}}

from which i got the value some thing like this

从中我得到了一些像这样的价值

pydev.aviesta.com

and

pydev.aviesta.com.mx

i need to show different data for both domains but as this is the dev server i cant use the full doamin name for compare can i check only .mx or .com so there will be no problem when going to live site

我需要显示两个域的不同数据,但因为这是开发服务器,我不能使用完整的doamin名称进行比较我可以只检查.mx或.com所以进入实时网站时没有问题

2 个解决方案

#1


1  

You may need Custom filter for this.

您可能需要自定义过滤器。

@register.filter(name='split')
def split(value, arg):
    return value.split('.')[-1]

Use it as {{request.META.HTTP_HOST|split}}

将其用作{{request.META.HTTP_HOST | split}}

#2


0  

i got the solution with js

我用js得到了解决方案

<script>
 var str = location.host;
        if( str.search(".mx") > 0 ){
        var dom = 'mx';
        }else{
        var dom = 'com';
        } </script>

#1


1  

You may need Custom filter for this.

您可能需要自定义过滤器。

@register.filter(name='split')
def split(value, arg):
    return value.split('.')[-1]

Use it as {{request.META.HTTP_HOST|split}}

将其用作{{request.META.HTTP_HOST | split}}

#2


0  

i got the solution with js

我用js得到了解决方案

<script>
 var str = location.host;
        if( str.search(".mx") > 0 ){
        var dom = 'mx';
        }else{
        var dom = 'com';
        } </script>