如何从html源代码中删除空格

时间:2021-05-12 06:08:48

I am using django and python. In the template file, I have a drop-down list which is shown as below. It works. The only problem is that there is a lot of white space between in the source html code. Is there any way to remove the white space? Thanks.

我正在使用django和python。在模板文件中,我有一个下拉列表,如下所示。它的工作原理。唯一的问题是源html代码之间有很多空白。有没有办法去掉空白?谢谢。

           {% for lang_ele in video.languages.all %}
              {% ifequal lang_ele.lang display_language  %}
                   {% for key, value in language_table.items %}
                       {% ifequal lang_ele.lang key%}
                         <option selected = "selected" value={{lang_ele.lang}}>{{value}}</option>
                       {% endifequal %}
                   {% endfor %}
              {% else %}
                   {% for key, value in language_table.items %}
                       {% ifequal lang_ele.lang key%}
                         <option value={{lang_ele.lang}}>{{value}}</option>
                       {% endifequal %}
                   {% endfor %}

              {% endifequal %}
           {% endfor %}

The output html souce code looks like this:

输出的html souce代码如下:

<option value=de>German</option> 


<option value=el>Greek</option> 


<option value=hi>Hindi</option> 

3 个解决方案

#1


45  

You can use the spaceless template tag. It:

您可以使用无空格模板标记。它:

Removes whitespace between HTML tags.

删除HTML标记之间的空格。

{% spaceless %}
<p>
    <a href="foo/">Foo</a>
</p>
{% endspaceless %}

#2


3  

  1. Look toward middelware, eg "htmlmin". It processes the file at a time. In addition it has a decorator. https://crate.io/packages/django-htmlmin

    看看中间的软件,如“htmlmin”。它一次处理文件。此外,它还有一个装饰者。https://crate.io/packages/django-htmlmin

  2. gzip will give the same effect. Probably would have cost to opt out of trimming. Look towards django middelware or nginx gzip.

    gzip会产生同样的效果。可能会有成本,选择不削减。看看django middelware或nginx gzip。

  3. You use the controller/model logic in the template. This is wrong way.

    在模板中使用控制器/模型逻辑。这是错误的方式。

#3


1  

I used custom helper function with stripping. Here is a an example I used too: http://justcramer.com/2008/12/01/spaceless-html-in-django/

我使用了自定义帮助函数和剥离。这里有一个我也用过的例子:http://justcramer.com/2008/12/01/spaceless-html-in-django/

#1


45  

You can use the spaceless template tag. It:

您可以使用无空格模板标记。它:

Removes whitespace between HTML tags.

删除HTML标记之间的空格。

{% spaceless %}
<p>
    <a href="foo/">Foo</a>
</p>
{% endspaceless %}

#2


3  

  1. Look toward middelware, eg "htmlmin". It processes the file at a time. In addition it has a decorator. https://crate.io/packages/django-htmlmin

    看看中间的软件,如“htmlmin”。它一次处理文件。此外,它还有一个装饰者。https://crate.io/packages/django-htmlmin

  2. gzip will give the same effect. Probably would have cost to opt out of trimming. Look towards django middelware or nginx gzip.

    gzip会产生同样的效果。可能会有成本,选择不削减。看看django middelware或nginx gzip。

  3. You use the controller/model logic in the template. This is wrong way.

    在模板中使用控制器/模型逻辑。这是错误的方式。

#3


1  

I used custom helper function with stripping. Here is a an example I used too: http://justcramer.com/2008/12/01/spaceless-html-in-django/

我使用了自定义帮助函数和剥离。这里有一个我也用过的例子:http://justcramer.com/2008/12/01/spaceless-html-in-django/