Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django replace html href and src to a {% static %} tag

how I can django replace html href and src to a static tag in Pycharm? For example:

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

into

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

thx!

like image 714
Alex F. Avatar asked Mar 06 '23 18:03

Alex F.


1 Answers

You can find and replace into the project with Ctrl + Shift + R.

Into the file selection, you can specify that you want to perform replacements in *.html files, and that we want to search for a regex (check the checkbox Regular expression).

As pattern, you can write a pattern like:

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

and as patttern to replace:

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

You might want to consider tweaking the pattern a bit, for example to only replace paths that start with libs/, etc.

this will then propose the changes. I advise you to look through these manually since there can be false positives: proposed changes, that should not change.

like image 75
Willem Van Onsem Avatar answered Mar 18 '23 11:03

Willem Van Onsem