Login

Top-rated snippets

Snippet List

HttpResponseSendfile

An HttpResponse for giving the user a file download, taking advantage of X-Sendfile if it's available, using FileWrapper if not. Usage: HttpResponseSendfile('/path/to/file.ext') To bypass the fallback: HttpResponseSendfile('/path/to/file.ext', fallback=False) This has been tested working with Lighttpd 1.4.28's mod_fastcgi.

  • download
  • sendfile
  • x-sendfile
Read More

Django enumeration for model field choices

The problem with supplying a Django model field with choices parameter is the way you check a value of that field in an object. You do nasty things like this: if model_instance.choice_field == 1: The problem of getting rid of hard-coded numbers is recognized over the internet, but I haven't found any short and understandable solution. Basically, we need a enumeration in python, that is ok to use as the Django `choices` model field argument. I've seen a couple of solutions of DjangoSnippets. Mine is shorter and easier because it only works for integer field choices.

  • choices
  • integer
  • enumeration
Read More

Encrypted paypal buttons

Function and usage in views and template with django-paypal to have encrypted paypal buttons with a cart(adding multiple elements). All credits go to Jon Atkinson, http://jonatkinson.co.uk/paypal-encrypted-buttons-django/ I just added it here with a complete implementation using a cart(his example didnt include it). I know there is some redundancy in the data passed to the dict and the submit form, i'm just not sure what can i take out, the paypal docs are not clear about it, if you test this code without some of the data and it works, please tell me. The key parts are the cmd _s-xclick and again the cmd '_cart' both are needed.

  • paypal
  • encrypted button
  • cart
Read More

Handles Inline Formsets and also "in-standard-way" normal forms

If you read the docstring and the example you should get a clue what this Code does. I didn't want a big function everytime that handles every specific form, formset combinations so this how i can add/edit Models with specific Forms given to the magic_handle_inlineformsets function. It also works for Forms without innline_formsets.

  • forms
  • formset
  • inline
  • inlineformset
  • inline_formset
Read More

One register on admin

This code is for set one register on admin if exist more than 1 register you can not delete it or add more. Only set mymodel

  • admin
  • no-add
  • no-delete
  • one-line
  • one-row
  • one-register
Read More

"Open file in Textmate"-support in werkzeug debugger browser view

1) Install django-extensions (requires werkzeug) 2) Paste snippet into settings.py 3) manage.py runserver_plus Now you should be able to open files in textmate by clicking the file links in the werkzeug error pages. It will also take you to the correct line number and highlight files that are in your project directory in a different color.

  • debug
  • error
  • debugging
  • textmate
Read More

Skip only specified spaces

Similar to `{% spaceless %}`, but only removes spaces followed by a special marker (`&nosp;`). Helps keeping proper indentation: {% skipspaces %} Lots of coffee {% if from_brazil %} from Brazil {% else %} from Columbia {% endif %} {% if tea %}&nosp;, some tea{% endif %} and a cake. {% endskipspaces %} Otherwise you'd have to write it in one looong line or get used to living with spaces before commas :) NB the marker itself is stripped from rendered html as well.

  • spaceless
  • whitespace
  • skip
  • spaces
Read More

Pretty Paginator Tag

This is a modified version of the (http://djangosnippets.org/snippets/73/) paginator snippet. It works with the django paginator.

  • paginator
  • 'digg style'
Read More

autotranslate po files using microsoft translator

This snippet is inspired by [dnordberg](http://djangosnippets.org/snippets/1048/) 's translation script which used google's translation script. But after google translation is no more free. I put together it to use microsoft translator's python wrapper by [openlab](https://github.com/openlabs/Microsoft-Translator-Python-API) usage ----- 1. get a bing appID from [here](https://ssl.bing.com/webmaster/developers/appids.aspx) and replace it on the top of script 2. sudo apt-get install translate-toolkit 3. sudo pip install microsofttranslator 4. sudo autotranslate.py [pofile] [sourcelangcode] [targetlangcode] Enjoy!!! **Prabhat Kumar Gupta** Python/Django Dev [http://www.prabhatgupta.com](http://www.prabhatgupta.com)

  • internationalization
  • i18n
  • translation
  • po
Read More

Filter change list by a date range

The Django admin site has a feature to filter objects in change list by parameters supplied in the query string. Particularly, parameters such as date\__gte and date\__lte can be used. This example is for filtering objects in change list by a date range (the date field is called expiration_date, but you can, of course, use any other name). As the server side (Django) already supports such filtering, all you need to do is to edit this for your needs (you can also add some DRY if you want) and put it into templates/admin/app_name/model_name/change_list.html. Some CSS for better layout: div.date_filter select#from_month, div.date_filter select#to_month { margin-left: 0; } div.date_filter label { margin-right: 5px; }

  • javascript
  • date
  • jquery
  • html
  • change_list
  • change-list
  • date-range
Read More

Jquery ajax csrf framework for Django

1. Framework to extend the jquery ajax() function to construct post requests that contain a csrf token. 2. The example view used with the framework takes JSON data and returns JSON data containing either: 3. "success" with a message and additional dictionary of JSON data to use in the page 4. "error" with an error message. 5. The ajax function framework satisfies Django's csrf requirements by injecting a csrf token into the post requests created using the function. This example is a form with ~160 fields that we wanted to help fill in customer information to automatically. 1. User calls the lookup() script from the onblur attribute of the customer_id form field by leaving the field. 2. The lookup script takes the contents of the customer_id formfield and uses the jquery ajax() function to construct a JSON post request to the "/json /?act=" url. 3. The json view takes actions as get requests. We pass the post request to the JSON url already including the get request. "/json/?act=lookup" 4. The jquery framework in the snippet includes a csrf token in the ajax request automatically. 5. The customer_id is passed as JSON to the json view lookup action and customer details are attempted to be looked up in the database. 6. If successful the request returns a JSON dictionary of customer details which are pushed into the formfields using javascript in the lookup() function. The end result is if the user fills out the customer_id field of the form first (which we suggest with tooltip overlay) the customer name and address information will populate automatically. *Credit to Guangcong Luo https://github.com/Zarel

  • json
  • jquery
  • csrf
Read More

3110 snippets posted so far.