Ramiro Jr. Franco
Ramiro Jr. Franco

Reputation: 286

How do I strip tags that have already been filtered?

I have this model being created by a third party, and I need to access this model and remove tags, I thought I could just use the |removetag filter, but it seems the tags are pre-escaped for the model, anyone have some advice on how else I could remove these tags from the template side?

Upvotes: 2

Views: 517

Answers (1)

DrMeers
DrMeers

Reputation: 4207

It depends on what you mean by 'already filtered'; can you provide any example string?

Does running it through |safe|removetags help?

I imagine you're also aware that you can use |striptags if you wish to remove all tags.

EDIT: Example data uploaded.

OK, if your strings look like that, you'll need to create the reverse of the django "escape" filter -- e.g. in one of your apps create a templatetags module, create unescape.py:

from django import template
from django.utils.safestring import mark_safe
from django.utils.encoding import force_unicode

register = template.Library()

@register.filter
def unescape(value):
    return mark_safe(force_unicode(value).replace('&lt;', '<').replace('&gt;', '>').replace('&quot;', '"').replace('&#39;', "'").replace('&amp;', '&'))

Don't forget to create an __init__.py in the templatetags directory also, and then restart your server so it gets registered.

Then in your templates:

{% load unescape %}
...
{{ example_string|unescape }}

See http://docs.djangoproject.com/en/dev/howto/custom-template-tags/ for better instructions if you haven't done this before.

Alternatively you could just run this on your models in python code somewhere, but your question asked about "the template side".

Upvotes: 1

Related Questions