89👍
I really hate doing all this junk by hand, so I wrote a sed script to do it for me. Make sure you have a backup first, then run this in your templates directory:
find . -type f -print0 | xargs -0 sed -i 's/{% url \([^" >][^ >]*\)/{% url "\1"/g'
It’ll go through all of your template files and replace this:
{% url something.else foo bar %}
with this
{% url "something.else" foo bar %}
Be careful, I was a little lazy with this, it might break on some constructs. It’s still going to be easier looking for errors in a diff than doing it by hand, though.
4👍
To exclude folder of .git and to avoid error’s MacOS added empty quotes to option -i ”. Example:
find . -path '*/.git*' -prune -o -type f -print0 | xargs -0 sed -i '' 's/ url \([^" >][^ >]*\)/ url "\1"/g'
But I like this approach (MacOS):
grep '{% url' -lrZ . | xargs -0 sed -i '' 's/ url \([^" >][^ >]*\)/ url "\1"/g'
- [Django]-Django.db.utils.IntegrityError: duplicate key value violates unique constraint "django_migrations_pkey"
- [Django]-How to add multiple objects to ManyToMany relationship at once in Django ?
- [Django]-Not able to create super user with Django manage.py
3👍
Firstly, you were correct to use single quotes for the view name, i.e. 'auto.views.view_post'
.
Now, temporarily remove the url
tag, and check that {{ post }}
and {{ post.slug }}
give you the values you expect. The error message arguments '('',)'
suggests that post.slug
is the problem.
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
- [Django]-Django 1.7 migrate gets error "table already exists"
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
0👍
If you are using Mac OS you’ll need to pass -e
find . -type f -print0 | xargs -0 sed -i -e 's/ url \([^" >][^ >]*\)/ url "\1"/g'
- [Django]-Checking for empty queryset in Django
- [Django]-Distributed task queues (Ex. Celery) vs crontab scripts
- [Django]-Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?
0👍
I was getting an “ILLEGAL BYTE SEQUENCE” error from sed with most of these recipes, which I was able to fix by doing this first:
LANG=C
However, these recipes generated tons of false positives on my project, and we already had a mix of url names that were single-quoted, double-quoted, or unquoted. It was a mess. Turned out the cleanest approach was to just search the templates directory with my editor (Sublime) in regex mode for:
\{\%\ url\ [^']
\{\%\ url\ [^"]
(find all instances that weren’t already quoted) and go through them visually. That turned out to be faster and cleaner than trying to automate it and then clean up the mess afterwards.
- [Django]-Force django-admin startproject if project folder already exists
- [Django]-Django: How can I protect against concurrent modification of database entries
- [Django]-CSRF Failed: CSRF token missing or incorrect
0👍
you may also need to do regexp-replace "\{% url "([\w:]+)"
for "\{% url '$1'
in order to aviod syntax errors like <a href="{% url "foo:bar" %}">baz</a>
- [Django]-Request.POST.get('sth') vs request.POST['sth'] – difference?
- [Django]-Making django server accessible in LAN
- [Django]-Add a non-model field on a ModelSerializer in DRF 3