Login

Most bookmarked snippets

Snippet List

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

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

P3P Headers for iframes

This is nothing fancy and hasn't much to do with django itself, I just searched for this information for quite a while and thought it may be useful for others. If you use IE7 (and maybe IE6), it will block cookies in iframes, if the iframes content comes from another server (quite common, I think). The P3P specification lets you declare your privacy settings in a format interpretable by browsers, essentially you can tell IE that you adhere to "don't be evil", and are allowed to handle cookies afterwards. I don't think that makes much sense, but it seems that it is the only way to make IE accept cookies in iframes. I had no idea that django made it that incredibly easy to "patch" the response-header, but it does! :)

  • p3p
  • ie7
  • internet
  • explorer
  • iframe
  • privacy
Read More

Template range tag

This is a simple tag that I am sure has been written before, but it helps people with the problem, 'how do I iterate through a number in the tempaltes?'. Takes a number and iterates and returns a range (list) that can be iterated through in templates Syntax: {% num_range 5 as some_range %} {% for i in some_range %} {{ i }}: Something I want to repeat\n {% endfor %} Produces: 0: Something I want to repeat 1: Something I want to repeat 2: Something I want to repeat 3: Something I want to repeat 4: Something I want to repeat

  • template
  • context
  • range
Read More

MySQL "Text" Type Model Field

Custom field for using MySQL's `text` type. `text` is more compact than the `longtext` field that Django assigns for `models.TextField` (2^16 vs. 2^32, respectively)

  • text
  • models
  • mysql
  • db
  • database
  • field
  • custom-field
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

Get child model

With the child_object function defined in the parent model, you can get it's child object.

  • model-inheritance
Read More

Integrate Tenjin using a decorator

This provides basic [Tenjin](http://www.kuwata-lab.com/tenjin/) integration, using a decorator. Tenjin is a blazingly fast templating system, the fastest pure-python system available. For usage and configuration, see the example in the code. I modeled this after Robert Thomson's code for Mako integration.

  • decorator
  • tenjin
  • template-engine
Read More

exception handling middleware

<code> is_loaded = False if not is_loaded: is_loaded = True #... ExceptionHandlingMiddleware as EHM import views EHM.append(views.AuthFailError, views.auth_fail) # when AuthFailError thrown it redirects to `auth_fail` view function. </code>

  • middleware
  • error
  • exception
  • custom
  • handling
Read More

Conditional template parsing block

I'm developing a regionalization extension based on PostGIS and GeoDjango for a web-based LCA software. Because PostGIS (or PostgreSQL) is not available everywhere, we needed a way to fully disable the geographic extensions. Because of that, I set out to create a template block tag which only gets evaluated when GIS support is active and totally ignored (treated as a comment) otherwise. I had some problems getting my BoringNode to work (which should just pass the content through), so I've just used the AutoEscapeControlNode (why? Because it was the first usable class that jumped into my eye.) for this purpose.

  • template-tag
  • conditional-parsing
Read More
Author: mk
  • 0
  • 0

Who's helping a lot in IRC

We had some fun today on the #django IRC channel searching and counting through past logs for people saying "thanks" to [a known very helpful person](http://djangopeople.net/magus/). Here's a unix shell script for checking your own score if you're using Pidgin and have logging turned on. Replace ".purple" with ".gaim" in the script if you're using Gaim (an older version of Pidgin).

  • log
  • help
  • irc
  • thanks
  • score
Read More

sharedance sessions backend

"Sharedance is a high-performance server that centralize ephemeral key/data pairs on remote hosts, without the overhead and the complexity of an SQL database." [Frank DENIS](http://sharedance.pureftpd.org/project/sharedance)

  • session
  • backend
Read More
Author: d2
  • 0
  • 0

3110 snippets posted so far.