【问题标题】:django replace html href and src to a {% static %} tagdjango 将 html href 和 src 替换为 {% static %} 标签
【发布时间】:2018-12-30 06:54:30
【问题描述】:

我如何在 django 中将 html href 和 src 替换为 Pycharm 中的静态标签? 例如:

<link rel="stylesheet" href="libs/owlcarousel/assets/owl.carousel.min.css">
<a href="/"><img src="img/logo.png" alt=""></a>

进入

<link rel="stylesheet" href="{% static 'landing/libs/owlcarousel/assets/owl.carousel.min.css' %}">
<a href="/"><img src="{% static "landing/img/logo.png" %}" alt=""></a>

谢谢!

【问题讨论】:

    标签: html regex django django-templates pycharm


    【解决方案1】:

    您可以使用 Ctrl + Shift + R 查找并替换到项目中。

    在文件选择中,您可以指定要在*.html 文件中执行替换,并且我们要搜索正则表达式(选中复选框正则表达式 em>)。

    作为模式,您可以编写如下模式:

    \b(src|href)="([^"]*)"\b
    

    并作为要替换的模式:

    $1="{% static '$2' %}"
    

    您可能需要考虑稍微调整一下模式,例如只替换以libs/ 开头的路径等。

    然后这将提出更改。我建议您手动查看这些内容,因为可能存在误报:建议的更改,不应该更改。

    【讨论】:

    • 这种模式在 Pycharm 2020.1 中对我不起作用 你能建议调整一下 Willem 吗?会有很大帮助
    • 你能为此推荐一个 LINUX sed 命令吗?
    猜你喜欢
    • 2012-10-19
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    相关资源
    最近更新 更多