Alexander Seredenko
Alexander Seredenko

Reputation: 819

Modify data before render template in Django

What I need to do it's just replace some items from model before render. So just in each business.address replace '||' with ", ". I'm trying to do like that:

def category(request, q):
     businesses = Business.objects.filter(category_string__icontains=q)[:50]

     for b in businesses:
         if '||' in b.address:
             b.address.replace('||', ', ')
             print(b.address)

But I still see the same string without replacing. What's the reason?

Upvotes: 1

Views: 340

Answers (1)

heemayl
heemayl

Reputation: 42017

Strings are immutable in Python; hence, str.replace is not in-place.

You need to do the name binding (again):

b.address = b.address.replace('||', ', ')

Upvotes: 1

Related Questions