Login

3110 snippets

Snippet List

Custom requests auth class for Tastypie API key authentication

In case you ever use [requests](http://python-requests.org/) (or [slumber](http://slumber.in/)) to do requests against a Tastypie API that requires API key authentication, this small custom auth class will help you. Use it like that (with requests): auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252') response = requests.get('http://api.foo.bar/v1/spam/', auth=auth) or with slumber: auth = TastypieApiKeyAuth('jezdez', '25fdd0d9d210acb78b5b845fe8284a3c93630252') api = slumber.API("http://api.foo.bar/v1/", auth=auth) spam = api.spam().get()

  • api
  • auth
  • tastypie
Read More

Django Sudo

Staff can log in as a user, from a url to help with customer support or debugging.

  • admin
  • user
  • login
  • staff
  • sudo
Read More

MultiFormWizard

This is an extended version of the FormWizard which allows display of multiple forms per step and allows usage of ModelForms with initial objects

  • multiple
  • forms
  • form
  • wizzard
  • multi-form-per-step
Read More

Extended Form Wizard with ability to go backwards

This is an extended version of django wizard with the ability to go back and execute any step directly. To define next step to execute use the form field with the name "wizard_next_step". Don't forget to specify in your form the wizard_max_step field, so the knows the step number with the highest number, where the user was. An other improvement is the variable "wizard_data". It's a QueryDict with data from all wizard forms. It can be used to retrieve values from the field values of the forms from other steps. It could be helpfully for the latest step, where the user should see an overview of his input.

  • forms
  • wizard
Read More

Include captchas from recaptcha.net

Recaptcha is a free service that helps you protect your forms against spam bots by displaying a picture showing 2 words or playing an audio file telling 8 digits for the visually handicapped. After registration on http://recaptcha.net a private and a public key are generated. Put the keys in settings.py. Find client code for recaptcha at http://pypi.python.org/pypi/recaptcha-client. Put the file captcha.py into application root.

  • captcha
  • recaptcha
Read More
Author: b23
  • 8
  • 29

Very simple email image embed

This is a simple way to embed images in emails, rather than use absolute links, which many clients will not show by default. It has not undergone extensive testing but it should get you started. Comments / suggestions welcome.

  • image
  • email
  • embed
  • mime
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

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

Database template loader

This snippet provides getting templates from the model in database. We work with templates as usual (using as a template name value of the field **"slug"**). You can do your own application without "TemplateTypes" model - it's added for ability to filter templates. You can use choices or remove "template_type" field and "TemplateTypes" model at all. For ease of editing, you can connect all this to the admin interface, adding to the field "template_body" widget with syntax highlighting (I used [CodeMirror](http://codemirror.net/)).

  • templates
  • template loader
Read More

Overcome the bulk_create() size limitation using SQLite

As of django 1.4, the newly introduced `bulk_create()` function has a strong limitation when using SQLite. Namely it causes an error if you want to create more than `999/F` objects, where `F` is the number of fields in the object type. The above `safe_bulk_create(objs)` function solves this issue by splitting the list of objects to appropriately sized bulks. This solution also works fine with other db backends, and according to my experiments, it causes no significant overhead comparing to using `bulk_create()` directly. For more details on the issue, see https://code.djangoproject.com/ticket/17788 Thanks to charettes for pointing out how to calculate the number of fields in an object.

  • bulk_create
Read More

DRY menu Custom Template Tag

This is the description of a custom template tag to create DRY menu. It solves the problem of markup duplication in templates of your site. The menu always has one active option and one or several inactive options. HOW TO USE Define a structure of your menu in a parent template: {% defmenu "menu1" %} {% active %}<span class='active'>__text__</span>{% endactive %} {% inactive %}<a href='__url__'>__text__</a>{% endinactive %} {% opt "opt1" "/opt1/" %}Go to opt1{% endopt %} {% opt "opt2" "/opt2/" %}Go to opt2{% endopt %} {% opt "opt3" "/opt3/" %}Go to opt3{% endopt %} {% enddefmenu %} The menu has it's name (first parameter of the tag 'defmenu'. First parameter of a tag 'opt' is menu option's name. '__text__' inside of 'active'/'inactive' will be substituted by inner text of a tag 'opt' (Go to opt...), '__url__' indide of 'active'/'inactive' will be substituted by second parameter of a tag 'opt' To generate menu with one selected option in child template do: {% menu "menu1" "opt1" %} Here: "menu1" is a name of menu that was defined by 'defmenu' tag, "opt1" is selected option. Result of the applying 'menu' is the next: <span class='active'> Go to opt1</span> <a href='"/opt2/"'>Go to opt2</a> <a href='"/opt3/"'>Go to opt3</a>

  • menu
  • dry
  • menubar
Read More