Removes all target="..." attributes form hyperlinks, and then adds target="_blank" to all links starting with "http"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class ExtlinksBlankMiddleware(object):
'''
Makes sure all external and only exernal links open in a new window.
'''
def __init__(self):
self.targets = re.compile(r'''target=.\w*.''')
self.extlinks = re.compile(r'''<a (?P<old>[^>]*http.?://)''')
def process_response(self, request, response):
if ("text" in response['Content-Type']):
response.content = self.targets.sub('', response.content)
response.content = self.extlinks.sub('<a target="_blank" \g<old>',response.content)
return response
else:
return response
|
More like this
- Form field with fixed value by roam 4 days ago
- New Snippet! by Antoliny0919 1 week, 3 days ago
- Add Toggle Switch Widget to Django Forms by OgliariNatan 2 months, 4 weeks ago
- get_object_or_none by azwdevops 6 months, 3 weeks ago
- Mask sensitive data from logger by agusmakmun 8 months, 2 weeks ago
Comments
Well done!
#
Please login first before commenting.