Django Replace with space from underscore in templates
django, django-templates, python
Solution
If this should be the default output when using `{{ object }}`, then you should override the `__unicode__` method of your model like this:
class MyModel(models.Model):
title = models.CharField(...) # this field may hold our string "New_Mexico"
...
def __unicode__(self):
return self.title.replace('_', ' ')
A different approach might be using `{{ object.human_readable_title }}`, where you define a method in your model:
def human_readable_title(self):
return self.title.replace('_', ' ')
I see no reason to build your own template tag for this use case, except you want to use it for many fields of different models.
EDIT: It seems you have a field called "state" in your "jobs" class. So in your case, a solution might be: in your model class create a method
def human_readable_state(self):
return self.state.replace('_', ' ')
and in the template use:
<label>{{ d.human_readable_state }}</label>
That should do the trick.
Problem
In my database jobs table, state field having values "New_Mexico". But I need to display in html page like "New Mexico". Give some ideas for find and replace. views.py ``` def result(request,user_id): userid=user_id query = Q(emp__companyname=userid) | Q(state=userid ) details=jobs.objects.filter(query).select_related() return render_to_response('result.html', {'details': details}) ``` result.html ``` {% for d in details %} <label>{{ d.state }}</label> {% endfor %} ```