Login

All snippets written in Python

Snippet List

Command to make fixtures.

"Make fixture" command. Highly useful for making test fixtures. Use it to pick only few items from your data to serialize, restricted by primary keys. By default command also serializes foreign keys and m2m relations. You can turn off related items serialization with `--skip-related` option. How to use: python manage.py makefixture will display what models are installed python manage.py makefixture User[:3] or python manage.py makefixture auth.User[:3] or python manage.py makefixture django.contrib.auth.User[:3] will serialize users with ids 1 and 2, with assigned groups, permissions and content types. python manage.py makefixture YourModel[3] YourModel[6:10] will serialize YourModel with key 3 and keys 6 to 9 inclusively. Of course, you can serialize whole tables, and also different tables at once, and use options of dumpdata: python manage.py makefixture --format=xml --indent=4 YourModel[3] AnotherModel auth.User[:5] auth.Group

  • serialize
  • admin
  • model
  • fixtures
  • tests
  • test
  • management
  • commands
  • fixture
  • command
  • make
Read More

Type less with newforms admin

I recently converted a site with over 60 models to newforms admin. I like the seperation of the display from the defintion, but it does introduce quite a bit more typing which isn't in the spirit of Django DRY... I particular I got bored of typing admin.site.register(Model, ModelAdmin) Over and over again so I wrote this little bit of code which does a bit of introspection. It assumes that 1. You import all your models into your admin.py, ie from "myapp.models import *" 2. The admin class for Model is called ModelAdmin Put this snippet at the end of your admin.py I hope that saves someone a bit of typing!

  • admin
Read More
Author: ncw
  • -4
  • 4

change a widget attribute in ModelForm without define the field

I will change a model form widget attribute without define the complete field. Because many "meta" information are defined in the model (e.g. the help_text) and i don't want to repeat this. I found a solution: Add/change the widget attribute in the __init__, see example code.

  • newforms
  • forms
  • field
  • modelform
Read More

phpbb (2.x) authentication backend

This class not only checks an old-style phpbb 2.x password, when the user successfully logs in, it rehashes the (correct) password in the newstyle hash and saves it. Eradicating the old, quite unsafe stored md5 password.

  • authentication
  • backend
  • phpbb
Read More

FilteredSelect

Displays as an ordinary selectbox with an additional text-input for filtering the options. An adaption of the example at [1] for use with Django. The code assumes the java script from [2] is downloaded into your media folder. Remember to output your form's media, for instance like: "{{form.media|safe}}", somewhere above your form. **License:** [2] by Mr Patrick Fitzgerald is licensed under GPL and the python code written by myself is GPL as well. **References:** 1. http://www.barelyfitz.com/projects/filterlist/index.php/ 2. http://www.barelyfitz.com/projects/filterlist/filterlist.js

  • javascript
  • widget
Read More

Using Google Apps Premium infrastructure for user management

To create a lower entry barrier to logging into our intranet I created a very simple backend using Google Apps Premium provisioning API (http://code.google.com/apis/apps/gdata_provisioning_api_v2.0_reference.html). This enables controlling access for your users based on their status in your Google Apps instance. *(NOTE! Since the provisioning API is only available in the Premium version of Google Apps you first need to upgrade if you haven't done so already)* Requirements: You need Google Data libraries for python. They can be downloaded from http://code.google.com/p/gdata-python-client/downloads/list Google Apps Premium -- User used by the script must have admin rights

  • authentication
  • login
  • auth
  • google
  • google-apps
  • auth-backend
Read More

Models for Postal Addresses

Here's some fairly normalized models for representing postal addresses. Making postal_code a separate model would probably be the only way to get it to a level that everyone would agree is 2NF, but we handle a lot of international addresses so that isn't really feasible. Country and StateProvince are intended to be populated with data from ISO 3166. I'd include the SQL I used with that data, but it's very long and there's no attach feature. Also, I'd probably be violating ISO's copyright.

  • models
  • countries
Read More

Excel Spreadsheet Export

This is an example of a view that returns an Excel spreadsheet; the last ~7 lines are the relevant ones. It relies on Excel's ability to automatically import HTML (see http://support.microsoft.com/kb/165499 for more info). The spreadsheet.html template is just has one big `<table>` tag with all the data as table cells (no `<html>` or other surrounding tags are necessary).

  • export
Read More

Gzip decorator

Rather than using the full GZipMiddleware, you may want to just compress some views. This decorator lets you do that. @gzip_compress def your_view(request, ...): ....

  • middleware
  • decorator
  • gzip
Read More

PermanentRedirectMiddleware

This is a simple middleware that redirects the exactly URL requested with the correct domain. It is useful when you have more than one domain (most of cases with "www." or IP) to access a website. To make it works, download the snippet file as the name "permanent_redirect.py" and add its path as the first item in MIDDLEWARE_CLASSES setting in settings.py. Later you must inform a setting called `HTTP_HOST_DOMAIN` with the correct domain.

  • middleware
  • redirect
  • permanent
  • seo
Read More

PostGIS "nearest" including range limiting

this is an enhancement to paulsmith's code snippet 190, to provide an extra "search by distance" parameter rather than "limit number of results returned". i need this for a social networking site where users can search for people within a certain geographical area.

  • postgis
Read More

Credit Card With Newforms

Alternative version of newform code for handling credit cards. Unlike the other two credit-card snippets (http://www.djangosnippets.org/snippets/764/ and http://www.djangosnippets.org/snippets/830/), this uses two drop-down boxes instead of text fields for the expiration date, which is a bit friendlier. It doesn't do as much checking as snippet #764 since we rely on the payment gateway for doing that. We only accept Mastercard, Visa, and American Express, so the validation code checks for that.

  • newforms
  • datefield
  • credit-card
  • expiration-date
Read More

Caching XHTML render_to_response

This code improves on Django's render_to_response shortcut function in the following ways: 1. If the caller does not provide a MIME type, and the caller is passing a RequestContext, it interrogates the request to determine if the HTTP client supports application/xhtml+xml encoding. If so, it sets the request type to application/xhtml+xml to override the HttpRequest class's default mime type of text/html. 2. It caches parsed templates in its own cache to improve performance. If you aren't using XHTML in your templates, you may choose to comment out the code that tests for and sets the application/xhtml+xml MIME type. I place this code in a file named "util.py" in my application. In my views, I write "from app.util import render_to_response" where I used to write "from django.shortcuts import render_to_response". Note that the caching functionality provided by this code means that you will need to recycle your Django instance when you make template changes. Instructions for doing this depend on how you have deployed your Django application.

  • render_to_response
  • template
  • cache
  • html
  • xhtml
Read More

2956 snippets posted so far.