Login

Most bookmarked snippets

Snippet List

Improved User Admin

Helper function which adds some niceties to the auth/user admin views. Needs django 1.2 to work. ### Usage Define a UserProfile class and set `AUTH_PROFILE_MODULE` as per the [django docs](http://docs.djangoproject.com/en/dev/topics/auth/#storing-additional-information-about-users) In your admin.py file (or anywhere else) add the following: from models import UserProfile from [path to snippet] import upgrade_user_admin upgrade_user_admin(UserProfile)

  • admin
  • user
  • userprofile
Read More

Better Static Image Serving With Fallback

Serves images from a local directory, but if it doesn't find it locally, then go look for it on a server. This is useful for sites with very large amounts of static content. Instead of copying all your prod images to dev every time you update the database, simply use this in your URL patterns to serve the images: `(r'^(?P<path>.*)$', 'static_fallback.serve', {'document_root' : '/path/to/my/files/'})` By default, it caches the images locally in the path it was supposed to find them. The fallback server URL can be specified in urls.py or settings as FALLBACK_STATIC_URL. Thanks to Johnny Dobbins for the idea. Based on the Nginx pass through image proxy concept. For more info [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)

  • image
  • static
  • images
  • fallback
Read More

SearchableManager

A drop-in chainable manager for providing models with basic search features such as +/- modifiers, quoted exact phrases and ordering by relevance.

  • search
  • model
  • manager
  • queryset
Read More

SerializedObjectField

Model field that stores serialized value of model class instance and returns deserialized model instance. Example usage: from django.db import models import SerializedObjectField class A(models.Model): object = SerializedObjectField(serialize_format='json') class B(models.Model): field = models.CharField(max_length=10) b = B(field='test') b.save() a = A() a.object = b a.save() a = A.object.get(pk=1) a.object <B: B object> a.object.__dict__ {'field': 'test', 'id': 1}

  • django
  • models
  • fields
  • json
Read More

Read settings from local_settings.py

Each installation of our Django site has slightly different settings -- namely, which database to use. Developers can provide a `local_settings.py` file which lets them override (or, just as usefully, extend) settings that are in `settings.py`. Subversion is told to ignore `local_settings.py`, so it's never checked in. If `local_settings.py` is missing, the site refuses to work. We include a `local_settings_example.py` file so that new developers can get started more quickly.

  • settings
  • development
  • deployment
  • deploy
  • production
  • local
  • local-settings
  • local-deployment
Read More

Drop in dynamic formsets in admin

A very plugable way to get Stanislaus jquery dynamic formset working in the admin with adding just one template. Add the following to templates/admin/APP/MODEL/change_form.html and also update the MODEL in the prefix setting. Thanks Stanislaus [http://elo80ka.wordpress.com/2009/10/10/jquery-plugin-django-dynamic-formset/](http://elo80ka.wordpress.com/2009/10/10/jquery-plugin-django-dynamic-formset/) [http://go2.wordpress.com/?id=725X1342&site=elo80ka.wordpress.com&url=http%3A%2F%2Fcode.google.com%2Fp%2Fdjango-dynamic-formset%2F](http://go2.wordpress.com/?id=725X1342&site=elo80ka.wordpress.com&url=http%3A%2F%2Fcode.google.com%2Fp%2Fdjango-dynamic-formset%2F) [http://www.djangosnippets.org/snippets/1389/](http://www.djangosnippets.org/snippets/1389/)

  • dynamic
  • admin
  • jquery
  • inlines
  • formsets
Read More

All Imports Checker

I was using flup to run django in fcgi mode and encountered the dreaded "Unhandled Exception" page quite frequently. So apart from all the precautions about handling this except, I wrote the above code snippet, which checks the import across your ENTIRE project. Ofcourse this can be used on any python project, but I have written it for my favorite framework django. It is now written as a Django command extension, an can be run as: **python manage.py imports_checker** This is a generic command, it does not check the settings.INSTALLED_APPS setting for cleaning. But can be improved to do the same. Public Clone Url: [git://gist.github.com/242451.git](git://gist.github.com/242451.git) Update: Now it supports checking imports, just only at the app level also usage: python manage.py imports_checker &lt;appname&gt;

  • django
  • python
  • imports
Read More

tag: render form field

this solves a common problem where you want to specify html tag attributes for form fields in the template itself and not have to do it by writing a custom form class. eg. the size of the field, css classes, tabindex etc. usage: {% render_field form.comments "cols=40,rows=5,class=text,tabindex=2" %} where form.comments is a form field with a text area widget it will show data (if the form is bound or if there is initial data) and will display errors if the field has errors

  • forms
Read More

Git media cache busting tag

This tag appends the current git revision as a GET parameter to a media files so the web server can set an expires header far in the future. Your project must be structured such that MEDIA_ROOT/../.git exists. Usage: `<link rel="stylesheet" type="text/css" href="{% media myapp/css/base.css %}">`

  • cache
  • media
  • git
Read More

Ajax auto-filtered Foreign Key Field

Say you have a ModelChoiceField and you want the choices to depend on the value of another field in the form... that's what these bits are for. They need to be used in conjunction with the Ajax views from: [code.google.com/p/django-ajax-filtered-fields/](http://code.google.com/p/django-ajax-filtered-fields/) See my blog for full details: [anentropic.wordpress.com](http://anentropic.wordpress.com) ...um, this is work in progress and the explanatory blog post is not up yet...

  • ajax
  • widget
Read More

Stacked/Grouped Forms

This snippet provides support to create a bit more advanced forms, eg group fields together vertically and horizontally. It's a bit similar to the options one has about displaying fields in the admin.

  • forms
  • groups
  • stacked
Read More

Render Decorator

Simple decorator to render an html template or return a json response. The view must return a dict object. Usage example: @render('page.html') def a_view(request): #do something return {'key': 'value'}

  • render
  • decorator
Read More

Manager for multiple database connections

A Django model manager capable of using different database connections. Inspired by: * [Eric Florenzano](http://www.eflorenzano.com/blog/post/easy-multi-database-support-django/) * [Kenneth Falck](http://kfalck.net/2009/07/01/multiple-databases-and-sharding-with-django) There's a more detailed version in Portuguese in my blog: [Manager para diferentes conexões de banco no Django](http://ricobl.wordpress.com/2009/08/06/manager-para-diferentes-conexoes-de-banco-no-django/)

  • multiple
  • manager
  • database
  • connection
  • databases
  • connections
Read More
Author: rix
  • 0
  • 6

3110 snippets posted so far.