Login

Top-rated snippets

Snippet List

Auth decorators with 403

These decorators are based on user_passes_test and permission_required, but when a user is logged in and fails the test, it will render a 403 error instead of redirecting to login - only anonymous users will be asked to login.

  • auth
  • user_passes_test
  • 403
  • permission_required
Read More

Script for getting Google Page Rank of page

I was looking for such script written in python and found google checksum algorithm at http://pagerank.gamesaga.net/ And just added complete functionality to it. Usage: script.py PR http://somepage.com/page.html It also has function to retrieve Yandex TYC.

  • google
  • pagerank
  • yandex
Read More

newforms field callback helper

**Now redundant any anything >0.96**, as `form_for_*` methods now have a `fields` attribute `formfield_callback`s are a bit difficult to use, here's a helper method to create a callback function to use with the `form_for_instance` and `form_for_model` methods. Example usage: person_callback = new_callback(exclude=['password', 'can_add_staff', 'is_staff']) def form_for_person(person): return form_for_instance(person, formfield_callback=person_callback)

  • newforms
Read More

Handy tag for generating URLs to media files

Returns an absolute URL pointing to the given media file. The first argument is the path to the file starting from MEDIA_ROOT. If the file doesn't exist, empty string '' is returned. For example if you have the following in your settings: MEDIA_URL = 'http://media.example.com' then in your template you can get the URL for css/mystyle.css like this: {% media 'css/mystyle.css' %} This URL will be returned: http://media.example.com/css/style.css.

  • template
  • tag
  • media
Read More

Referer-checking view decorators

Here are a couple of Django decorators for limiting access to a view based on the request's `HTTP_REFERER`. Both raise a Django `PermissionDenied` exception if the referer test fails (or a referer simply isn't provided). The first, `referer_matches_hostname`, takes a hostname (and port, if specified) and matches it against the referer's. If multiple arguments are supplied a match against any of the hostnames will be considered valid. The second, `referer_matches_re`, takes a regex pattern (like Django's urlpattern) and tests if it matches the referer. This is obviously more flexible than `referer_matches_hostname` providing the ability to match not just the hostname, but any part of the referer url. Finally there's an simple example decorator, `local_referer_only`, that limits a view to the current site by using Django's `django.contrib.sites` to look up the current hostname.

  • view
  • referer
  • decorator
  • http_referer
  • request
Read More

"If in" templatetag

It's been a while, but I think I made this by copying and pasting the default {% if %} tag and modifying it. Does what it says, lets you test for inclusion in a list, in templates, like so: {% ifin item list %}.

  • template
  • lists
  • tag
  • contains
Read More

media_url context variable

with this you can have context variables which know the media url and the urls of all your applications, if you need it. save the code as myapp/context_processors.py and add the following line to `TEMPLATE_CONTEXT_PROCESSORS` setting "mysite.myapp.context_processors.url_info", For each application you need to know the url set `MYAPP_URL` and add it to dict.

  • url
  • media
  • context
Read More

IfValueTag

Don't repeat yourself: when you wish to have a block of html with a variable value, but only if the variable is set, you can do this: {% ifvalue company.contact.email as email %} <h3>Email address</h3> <a href='mailto:{{ email }}'>{{ email }}</a> {% endifvalue %} Instead of this: {% if company.contact.email %} <h3>Email address</h3> <a href='mailto:{{ company.contact.email }}'>{{ company.contact.email }}</a> {% endifvalue %} The tags ifvalue and ifnotvalue are provided by this snippet. If you don't specify `as somename`, then the variable's value will be assigned to the name "value".

  • tag
Read More

jQuery ajax search

This is the result of my first tests with jQuery and Django. After entering a search term it gets search results using ajax and json. Then is uses the rather crude `result_table` function to generate a table of results. Django is on the serverside for generating json

  • ajax
  • jquery
Read More

Using Templates to Send E-Mails

This is a basic example for sending an email to a user (in this case, when they've signed up at a website) using the Django template framework. It's really quite simple - we're just using a plain text template instead of HTML, and using the output of the template's 'render()' method as the message body. Of course, in your project you won't blindly use data from request.POST! This example was first posted on [my blog at rossp.org](http://www.rossp.org/blog/2006/jul/11/sending-e-mails-templates/)

  • templates
  • email
Read More

Multilingual Models

A way to implement models with translatable content. The translatable field of the default language has no extension, like "title" and the translations have extensions postfixes "_<two-letter language code>", like "title_la" or "title_mn". Method get_title() in this case returns the translation of the currently chosen language or the title of the default language, if the translation is not set. The class XFieldList overrides the default list class and is used for modifying ordering and list_display settings according the chosen language. For example, when the German language is chosen, the list of translatable content objects will be ordered by German titles (not English). *At the time when the list of field names is assigned to ordering or list_display (at the import of the model), the currently chosen language is still not known. But the language is known when ordering and list_display lists are used in contributed administration or elsewhere.* The XFieldList returns the modified values of the passed-in list, when its methods/properties are triggered. XFieldList transforms field names ending "_" (except the ones beginning with "__", like "__str__") to appropriate translations according the currently chosen language. For example ['title_', 'content_', 'is_published'] will be changed to ['title', 'content', 'is_published'] for the English language and to ['title_lt', 'content_lt', 'is_published'] for the Lithuanian language. *The best practice is to put XFieldList into a separate file and import it from different apps whenever needed.* It's worth mentioning that one implementing this should also know about [Django internationalization](http://www.djangoproject.com/documentation/i18n/).

  • i18n
  • l10n
  • translations
  • multilingual
Read More

Using Pygments with reST

UPDATED: This now supports an argument for the initial header level. This is a modified version of `django.contrib.markup` that allows you to highlight code via [pygments](http://pygments.pocoo.org/). The code block can be used as: `Here's a paragraph, and a code example: .. code:: language *insert code here* continue with your normal document.` Setup: Insert the snippet into `mysite/templatetags/rest.py`, then add `mysite` to your installed apps in `settings.py`. In your template, `{% load rest %}` and `{{ mycontent|rest }}`.

  • pygments
  • rest
  • restructured-text
  • template-filter
Read More

PyIfTag - Just like python if expression

How to use it {% pyif i == 1 %} <p>i=1</p> {% elif i == 3 %} <p>i=3</p> {% else %} <p>other</p> {% endif %} Warning: For now, django don't support elif, so you can use it. And I'v submit a patch about to fix it ( #3090 ). If the patch is accepted, you can use elif tag.

  • tag
Read More

"Magic Link" Management Command

Django Management Command to print a "Magic Link" for one-click log-in. This is nice for people who project switch or don't want to remember passwords.

  • authentication
  • magic
  • management
  • command
Read More

3110 snippets posted so far.