Multi-file find and replace for {% url “quoted” %} tag in Django 1.5
Django 1.5 deprecates the old {% url %}
tag style and uses the new one. The new one needs the url name to be quoted. So instead of {% url public_index %}
it must be {% url "public_index" %}
.
You’ll get an error:
‘url’ requires a non-empty first argument. The syntax changed in Django 1.5, see the docs.
Instead of adding quotes to each one in each template manually, I wanted a multi-file find and replace regex. Examples abound of using find
with xargs
and sed
. But here’s a simple Python script and example usage to do just that.
Note that it updates the files in-place. Make sure you have a backup and/or they’re committed to version control just in case something goes wrong and messes up your templates. You’ve been warned.
Here’s a simple find and replace Python script:
import sys import os import re def main(argv=None): if argv is None: argv = sys.argv if len(argv) < 4: print """ Usage: %s find replace filename.txt [filename2.txt...] """ % argv[0] exit find = argv[1] replace = argv[2] filepaths = argv[3:] for filepath in filepaths: text = open(filepath).read() #print re.sub(find, replace, text) new_text = re.sub(find, replace, text) if new_text != text: print filepath open(filepath, 'w').write(new_text) if __name__ == '__main__': main()
I saved the file as replacer.py
.
Here's how I called it:
find path/to/templates/ -name "*.html" | xargs python path/to/replacer.py '{% url ([\w_-]+) ' '{% url "\1" '
The find
command finds all .html
files in our templates directory. xargs
passes the file names to our replacer
script along with the find and replace patterns. We're putting quotes around the first parameter which is the url pattern name and leaving other parameters untouched.
This pattern assumes you've got a space between {%
and url
, which is the standard practice in django. If not, you can change the pattern.
Hope this helps.
sed(1) is much faster.
Comment by Patryk Zawadzki — 3 April 2013 @ 2:01 pm
Or you could just use VIM:
:args **/*.html
:argdo %s/{% url \(\%[\w-_]\+\)/{% url ‘\1’/g | update
Comment by mawe — 3 April 2013 @ 2:15 pm