Softpedia
 


LINUX CATEGORIES:



GLOBAL PAGES >>
NEWS ARCHIVE >>
SOFTPEDIA REVIEWS >>
MEET THE EDITORS >>
WEEK'S BEST
  • Linux Kernel 3.9.2 / 3....
  • LibreOffice 3.6.6 / 4.0.3
  • MPlayer 1.1.1
  • systemd 204
  • Arch Linux 2013.05.01
  • Blender 2.67
  • KDE Software Compilatio...
  • CrunchBang Linux Stable...
  • Elementary OS 0.1 / 0.2...
  • SystemRescueCd 3.6.0
  • Home > Linux > Internet > HTTP (WWW)

    django-html_sanitizer 0.1.3

    Download button

    No screenshots available
    Downloads: 101  Tell us about an update
    User Rating:
    Rated by:
    NOT RATED
    0 user(s)
    Developer:

    License / Price:

    Last Updated:

    Category:
    Selwin Ong | More programs
    MIT/X Consortium Lic... / FREE
    August 23rd, 2012, 00:05 GMT
    ROOT / Internet / HTTP (WWW)

     Read user reviews (0)  Refer to a friend  Subscribe

    django-html_sanitizer description

    Provides a set of HTML cleaning utilities for django models, forms and templates

    django-html_sanitizer is a Django app that provides a set of utilities to easily sanitize/escape/clean HTML inputs in Django. This app is built on top of bleach, the excellent Python HTML sanitizer.

    Installation

    Add sanitizer to your INSTALLED_APPS:

    INSTALLED_APPS = (
     # other apps
     "sanitizer",
    )


    Model Usage

    Similar to bleach, django sanitizer is a whitelist (only allows specified tags and attributes) based HTML sanitizer. Django sanitizer provides two model fields that automatically sanitizes text values; SanitizedCharField and SanitizedTextField.

    These fields accept three extra arguments: - allowed_tags: a list of allowed HTML tags - allowed_attributes: a list of allowed HTML attributes - strip: a boolean indicating whether offending tags/attributes should be escaped or stripped.

    Here's how to use it in django models:

    from django.db import models
    from sanitizer.models import SanitizedCharField, SanitizedTextField

    class MyModel(models.Model):
     # Allow only < a >, < p >, < img > tags and "href" and "src" attributes
     foo = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
     allowed_attributes=['href', 'src'], strip=False)
     bar = SanitizedTextField(max_length=255, allowed_tags=['a', 'p', 'img'],
     allowed_attributes=['href', 'src'], strip=False)


    Form Usage

    Using django sanitizer in django forms is very similar to model usage:

    from django import forms
    from sanitizer.forms import SanitizedCharField, SanitizedTextField

    class MyForm(forms.Form):
     # Allow only < a >, < p >, < img > tags and "href" and "src" attributes
     foo = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
     allowed_attributes=['href', 'src'], strip=False)
     bar = SanitizedTextField(max_length=255, allowed_tags=['a', 'p', 'img'],
     allowed_attributes=['href', 'src'], strip=False)


    Template Usage

    Django sanitizer provides a few differents ways of cleaning HTML in templates:

    - escape_html template tag

    Example usage:

    {% load sanitizer %}
    {% escape_html post.content "a, p, img" "href, src" %}


    Assuming post.content contains the string '< a href ="#" >Example< /a >< script >alert("x")< /script >', the above tag will output '< a href ="#" >Example< /a >'

    - strip_html template tag

    Example usage:

    {% load sanitizer %}
    {% strip_html post.content "a, p, img" "href, src" %}


    Assuming post.content contains the string '< a href ="#" >Example< /a >< script >alert("x")< /script >', the above tag will output '< a href ="#" >Example< /a >alert("x")'

    - escape_html filter

    Escapes HTML tags from string based on settings. To use this filter you need to put these variables on settings.py:

     SANITIZER_ALLOWED_TAGS - a list of allowed tags (defaults to an empty list)
     SANITIZER_ALLOWED_ATTRIBUTES - a list of allowed attributes (defaults to an empty list)

    For example if we have SANITIZER_ALLOWED_TAGS = ['a'], SANITIZER_ALLOWED_ATTRIBUTES = ['href'] in settings.py, doing:

    {% load sanitizer %}
    {{ post.content|escape_html }}


    Assuming post.content contains the string '< a href ="#" >Example< /a >< script >alert("x")< /script >', the above filter will output '< a href ="#" >Example< /a >'

    - strip_html filter

    Strips HTML tags from text, allowing only whitelisted tags/attributes. To use this filter you need to put these variables on settings.py:

    - SANITIZER_ALLOWED_TAGS - a list of allowed tags (defaults to an empty list)
    - SANITIZER_ALLOWED_ATTRIBUTES - a list of allowed attributes (defaults to an empty list)

    For example if we have SANITIZER_ALLOWED_TAGS = ['a'], SANITIZER_ALLOWED_ATTRIBUTES = ['href'] in settings.py, doing:

    {% load sanitizer %}
    {{ post.content|strip_html }}


    Assuming post.content contains the string 'Examplealert("x")', the above filter will output 'Examplealert("x")'


    Product's homepage

    Requirements:

    · Python
    · Django
    · Bleach

      


    TAGS:

    HTML cleaning | HTML sanitizer | Django | HTML | cleaning

    Go to top

    WindowsGamesDriversMacLinuxScriptsMobileHandheldNews

    SUBMIT PROGRAM   |   ADVERTISE   |   GET HELP   |   SEND US FEEDBACK   |   RSS FEEDS   |   UPDATE YOUR SOFTWARE   |   ROMANIAN FORUM