martes, 26 de enero de 2016

Setting up django-unfriendly with examples

Here we go one more time. This is a guide that extends the documentation of django-haystack and some troubleshooting.

Fitst things first, visit the "Read the docs" first.


Ok, so follow is the same "Read the docs", but with comments and some more examples, my comments are in bold blue.



The unfriendliest urls in town! django-unfriendly is a Django app that obfuscates urls and allows your application to natively execute the original view.
There is lots of talk about SEO friendly urls. The trend is towards more and more human readable information in your urls and Django makes it easy to create urls like:
But sometimes urls can give too much away. This is where django-unfriendly comes in.
django-unfriendly provides a template filter that obfuscates urls in your templates, and a url handler/view that deobfuscates and executes the original view (e.g. no redirection).


Perhaps you have a Django application with readable information in urls such as:
# original url
And you don't want nosy people trying to guess other urls:
# user guesses another url (that likely exists)
You can obfuscate the url which might look like:
# obfuscated url
Which will show the same response as the original url.


  1. The application must be completely transparent... Obfuscated urls should behave exactly like the original url. HTTP responses should be indistinguishable.
  2. The original url must never be exposed (no redirect).
  3. Tampering with the obfuscated url should be difficult. A tampered url should return a 404 - Page not found error.
  4. Obfuscated urls should be idempotent and may be safely cached. Or at least as cachable as the original url.


  1. Install the django-unfriendly package:
    # with pip
    pip install django-unfriendly
    # or with easy_install
    easy_install django-unfriendly
  2. The only dependency is pycrypto. Make sure it's installed:
    # with pip
    pip install pycrypto
    # or with easy_install
    easy_install pycrypto
  3. Add unfriendly to your INSTALLED_APPS:
  4. Add unfriendly.urls to your
    urlpatterns = patterns('',
        url(r'^u/', include('unfriendly.urls')),


Load the tag library into any templates where you want to use django-unfriendly:
{% load unfriendly_tags %}
Then apply the obfuscate filter to any url you'd like to hide:
<a href="{{ "/music/awesome/the-melvins/"|obfuscate }}">Melvins awesome</a>
Django would accept an href like: 
<a href="/music/awesome/the-melvins">Register Actor</a>

(notice that the last forward slash is missed '/')

However unfriendly with fail (with a blank page-no-error) with the missed forward slash, like:
<a href="{{"/music/awesome/the-melvins"|obfuscate}}">     <<<<-- No last forward slash

Unfriendly instead requires the forward slash, like:
<a href="{{"/music/awesome/the-melvins/"|obfuscate}}">

Or with {% url view %} reversal:
{% url as melvins_url %}
<a href="{{ melvins_url|obfuscate }}">Melvins awesome</a>
If SEO is still important to you, you can pass some SEO juice to the filter:
<a href="{{ melvins_url|obfuscate:"King Buzzo rocks" }}">Melvins awesome</a>


The following may be added to your to customize the behavior of this app.

    • default: True
    • Enables the template filter. When False, template filter returns original unaltered URL.

    • default: SECRET_KEY (well, first 32 bytes of it)
    • Random key used for encryption/decryption. Note: AES keys must be either 16, 24, or 32 bytes long.
    • default: SECRET_KEY (well, first 16 bytes of it)
    • Initial vector required by AES cipher. Note: AES initial vector must be 16 bytes long
    • default: True
    • Determines whether or not the decrypted data is validated against a crc checksum to detect tampering.

Add this to: yoursite/

#AES UNFRIENDLY_SECRET key must be either 16, 24, or 32 bytes long
#Initialization Vector is 16 bytes long, and has to be chanced in production vs dev
UNFRIENDLY_IV = 'g#revg3#*xw9j67h"

P.S. In the festivals it is common to see flags everywhere!

No hay comentarios:

Publicar un comentario