【问题标题】:pydev django template formattingpydev django 模板格式化
【发布时间】:2016-02-24 11:23:07
【问题描述】:

我习惯于使用 eclipse 自动格式化我的源代码。为此,我选择所有文本并按 Ctrl+Shift+F。 但是以 django 模板文件为例

{% extends "base.html" %} 
{% load staticfiles %} 
{% block content %}
<div class="row">
...

按 Ctrl+Shift+F 我会得到

{% extends "prototipo/base.html" %} {% load staticfiles %} {% block
content %}
<div class="row">

这会导致 TemplateSyntaxError。 有什么解决方法吗?

【问题讨论】:

    标签: django django-templates pydev


    【解决方案1】:

    与此同时,我开发了这个脚本,位于 tamplates 目录中。我在 Ctrl+Shift+F 之后运行它。

    import re
    import os
    
    def fix_template(filename):
        with open(filename,'r+') as f:
            content=f.read()
            for s in re.findall('(\{%((?!%}).)*\n.*%})',content):
                faulty_string=s[0]
                non_faulty_string=faulty_string.replace('\n',' ')
                content=content.replace(faulty_string,non_faulty_string)
            f.seek(0)
            f.write(content)
    
    basedir=os.path.dirname(os.path.abspath(__file__))
    
    for dirpath, dirnames, filenames in os.walk(basedir):
        for filename in filenames:
            if re.match('.*[.]html',filename) is not None:
                fix_template(os.path.join(dirpath,filename))
    

    【讨论】:

      【解决方案2】:

      嗯,那个编辑器来自 LiClipse(而不是 PyDev)对吧?

      我认为问题在于它使用默认的 HTML 代码格式化程序,而它应该使用 django-templates 感知代码格式化程序。

      请将此作为问题报告给 LiClipse (https://sw-brainwy.rhcloud.com/tracker/LiClipse) -- 尽管请注意 LiClipse 的处理方式是将这些操作委托给外部库(因此,HTML 格式有效,因为它在外部库中可用 --因此,在这种情况下,该选项可能是禁用格式化或查找实现它的库)。

      【讨论】:

        猜你喜欢
        • 2012-07-01
        • 2013-03-05
        • 1970-01-01
        • 2022-12-17
        • 2016-03-23
        • 2010-09-25
        • 2019-10-03
        • 2010-11-13
        相关资源
        最近更新 更多