Login

Top-rated snippets

Snippet List

Template tag for compressed CSS & JS (GAE version)

Extension of the idea from [WuzHere example from Google IO](http://code.google.com/p/wuzhere/) about creating one compressed js or css file. Original code used not very elegant if statements. I've changed it to template tags. On production server it will also use current version id. Insert code in *cssjs.py* file in *templatetags* dir in your application and use as below (more details in docs): `<script type="text/javascript" src="/media/jsmergefile.js"></script>` **This code does not compress CSS and JS on the fly, because GAE is read-only and using Datastore is too heavy.**

  • templatetag
  • google-app-engine
Read More

Export Models

Warning: This python script is designed for Django 0.96. It exports data from models quite like the `dumpdata` command, and throws the data to the standard output. It fixes glitches with unicode/ascii characters. It looked like the 0.96 handles very badly unicode characters, unless you specify an argument that is not available via the command line. The simple usage is: $ python export_models.py -a <application1> [application2, application3...] As a plus, it allows you to export only one or several models inside your application, and not all of them: $ python export_models.py application1.MyModelStuff application1.MyOtherModel Of course, you can specify the output format (serializer) with the -f (--format) option. $ python export_models.py --format=xml application1.MyModel

  • tool
  • dump
  • serialization
  • export
  • db
  • database
Read More

401 HttpResponse

This is a HTTP response is use in my application when I want to return a 401. It's pretty simple, but effective for my needs.

  • 401
  • httpresponse
Read More

Disable fields in oldforms admin using jQuery

This snippet shows how to disable fields in a edit page in the oldforms admin using jquery. The idea is to add the javascript to the edit page using the `js` attribute of the model's `Admin` class. In this case jQuery and a custom javascript file are added. The javascript sets the `disabled` attribute of the `name` field to `true` as soon as the document is ready.

  • admin
  • jquery
  • oldforms
  • disable
Read More

Custom optional abstract base attributes

I needed an abstract base class that can add attributes to the child classes based on the child's name. The attributes had to be implicit, but overridable, so all derived classes would get them by default, but they could be easily overriden in the child definition. So, the code code I came up with basically consists of a customized metaclass used by the abstract model.

  • model
  • abstract
Read More
Author: tie
  • 0
  • 2

Creating MySQL Alter table commands for Foreign Keys

When using mysql the sql that is generated by syncdb doesn't create the foreign key relationship in all cases. This code will run through a file called create_table.sql in which you store all your create sql statements ( use "python manage.py sqlall app1 app2 > create_table.sql" ) and outputs all the neccesary alter table scripts that add the foreign key. Its not 100% proof since the generated names can end up being more than 40 characters. Need to work on that. I have [written](http://vidyanand.wordpress.com/2008/06/16/is-it-a-mysql-or-django-fault/) about it a little more in detail.

  • mysql
  • foreign-keys
Read More

Use django-admin.py instead of manage.py

A lot of people new to Django don't realize that `manage.py` is [just a wrapper](http://www.djangoproject.com/documentation/django-admin/) around the `django-admin.py` script installed with Django and isn't needed. (You may need to symlink `django-admin.py` to someplace in your system `PATH` such as `/usr/local/bin`.) The most important thing it does is to set your `PYTHONPATH` and `DJANGO_SETTINGS_MODULE` environment variables before calling `django-admin.py`. Those same settings are needed when you move your site on to a production server like Apache, so it is important to know how they work. This shell function sets those variables for you. Put it in your `.zshrc` or bash startup script. It works for both the monolithic project style and the lightweight app style of Django development [[1](http://www.pointy-stick.com/blog/2007/11/09/django-tip-developing-without-projects/)], [[2](http://www.b-list.org/weblog/2007/nov/09/projects/)]. This function isn't fancy; drop a comment if you have an improvement. Written for zsh and tested with bash 3.1.17.

  • bash
  • shell
  • zsh
Read More

Simple views method binding

@match_func_by_method def frontpage (request) : pass def get_frontpage (request, argument, ) : # GET things pass def post_frontpage (request, argument, ) : # POST things pass

  • views
  • http-method
Read More

Mail logged errors to administrators

Activate this middleware and define `LOG_FORMAT` & `LOG_ROOTS` in your `settings.py`. Then you can use Python's `logging` module for easy logging within your application.

  • middleware
  • email
  • logging
Read More

make an unordered html list

This custom filter takes in a string and breaks it down by newline then makes each separate line an item in an unordered list. Note that it does not add the <ul> </ul> tags to give the user a chance to add attributes to the list. This is based of the 'linebreaks' filter built in django

  • html
  • list
Read More

module_from_path

I needed to dynamically import a module based on a path to that file on disk, without it necessarily being on the Python Path.

  • import
Read More

sort_by_id_sequence

I needed to sort a set of objects (a QuerySet for example) by an externally provided list of IDs - for example: >>> writers = Writer.objects.all() >>> sort_by_id_sequence(writers, [3, 1, 2]) [<Writer id: 3>, <Writer id: 1>, <Writer id: 2>]

  • sorting
Read More

decorator to add author to extra_fields in snippets 635

I had the need to add `request.user` to the `extra_fields` argument of quite a few of my view based on [create_update for newforms snippet](http://www.djangosnippets.org/snippets/635/). I could have wraped the `create_object` function in my views.py, but as the logic is always the same, it seemed like a good idea to Not Repeat Myself.

  • newforms
  • decorator
  • create_update
  • author
Read More

3110 snippets posted so far.