Login

Tag "django"

Snippet List

Haystack objects in one query

Actually the best way to handle this is to use built-in http://docs.haystacksearch.org/dev/searchqueryset_api.html#load-all I just missed it while checking documentation and wrote this crappy snippet!

  • django
  • haystack
  • SearchQuerySet
Read More

Manual CSRF check for Django Facebook canvas applications

The way to manually control CSRF correctness for FB applications. Automatic check cannot be used because FB does POST on your canvas URL when initializing your application without CSRF token. If you still want to use Django CSRF stuff do manual checks. You only need to perform manual check when there is no correct signed_request present in your request - correct request means you really deal with FB. Use facebook_csrf_check to verify POST requests when signed_request is absent.

  • django
  • python
  • post
  • facebook
  • csrf
  • fb
Read More

Configurable defaults for contrib.sites default Site during syncdb

This simple snippet provides a more sensible default for the Site object created during the first pass of syncdb (that is, with a default domain of `localhost:8000`). I made this so that the admin's "view on site" button will work automagically during my development cycle (which often involves dropping and recreating a sqlite database). In addition, it provides 2 options for configuring the default Site as you'd like: settings parameters (`DEFAULT_SITE_DOMAIN` and `DEFAULT_SITE_NAME`) or `kwargs` (the latter takes precedence).

  • django
  • admin
  • sites
  • syncdb
  • contrib
  • manage
  • Site
  • defaults
Read More

Class based view decorator

Converts a passed decorator to one that can be applied to a class based view (ie. automatically decorates dispatch). This means no more overriding dispatch for every view / request method you want to apply decorators to. Works in Django 1.3 but I suspect it probably works in 1.2 as well.

  • django
  • generic-views
  • decorator
  • class-based-views
Read More

A Django form field that accepts an uploaded image and creates a resized image attached with a push-pin

PushPinImageField is a Django form field that is a sub-class of an ImageField. The field accepts an image to upload and based on certain settings, notably the size of the resulting image, the sizes and colors of 3 different borders, as well as the color of the push pin, a re-sized image is created with a colorized push pin in the top-center. A live demo of the field as well as more detailed usage instructions are available as a [blog entry](http://www.robmisio.com/blog/2/).

  • django
  • image
  • thumbnail
  • form
  • field
  • custom-field
Read More

Pygmentify a code using template filter

The above snippet can be added as a custom filter for rendering code snippets in django templates. A simple '|render' next to any source code will do. To add css, use the following command to generate css file. pygmentize -S emacs -f html > pygments-colorful.css And link this css file to the template.

  • template
  • django
  • pygments
  • custom-filters
Read More

FirstRun Middleware

Simple piece of middleware that redirects all requests to **settings.FIRSTRUN_APP_PATH**, until a lockfile is created. Tested on 1.3 only, but I do not believe it requires any special functionality beyond that provided in 1.1 This is useful if you need to force a user to run an installer, or do some configuration before your project can function fully. At first glance, such a thing would generate a lot of hits on the disk. However, once the lockfile has been created, the middleware unloads itself, so when a project is in a production environment, the lockfile is only checked once per process invocation (with passenger or mod_wsgi, that's not very often at all). Once your user has completed your FirstRun app, simply create the lockfile and the project will function as normal. For it to function, the following settings must be configured: * **settings.PROJECT_PATH** - absolute path to project on disk (e.g. */var/www/project/*) * **settings.FIRSTRUN_LOCKFILE** - relative path of the lockfile (e.g. */.lockfile*) * **settings.FIRSTRUN_APP_ROOT** - relative URL of the App you want to FirstRun (eg.*/firstrun/*)

  • middleware
  • django
  • redirect
Read More

django_production.wsgi

The above conf file the easiest way to point the env and the settings together , the user has to point the django.wsgi file with the correct path and it should work fine and one can directly run it in apache

  • django
  • wsgi
Read More

(Almost) Single table polymorphism in Django

An emulation of "table per hierarchy" a.k.a. "single table inheritance" in Django. The base class must hold all the fields. It's subclasses are not allowed to contain any additional fields and optimally they should be proxies. They however may provide additional methods to operate on the declared field. The presented solution supports implicit inheritance, even across ForeignKeys, and ManyToManyFields. No additional database hits are imposed.

  • django
  • django-models
  • table-per-hierarchy
  • single-table-polymorphism
  • polymorphism
Read More

Human readable file names decorator

This is extremely simple decorator to add possibility to upload files with name specific in some object field. For example image with same name as object slug. Sample **model**: class Test(models.Model): image = models.ImageField(\ upload_to=upload_to_dest(path='pics/', \ human_readable_field='hrname')) hrname = models.CharField( \ max_length=128, \ blank=True, default='') Sample **form** for admin: FNAME_EXP = re.compile('^[A-Za-z0-9\-\_]+$') class TestAdminForm(forms.ModelForm): hrname = forms.RegexField( label="Human Readable File Name", \ regex=FNAME_EXP, \ help_text="""Allowed only latin alphabet (upper and lower cases), underscore and minus characters. PLEASE, DO NOT INCLUDE EXTENSION OF THE FILE. Sample: test-this-file""", \ required=False) class Meta: model = Test Sample *admin.py* for *Test* model: class TestAdmin(admin.ModelAdmin): form = TestAdminForm admin.site.register(Test, TesetAdmin)

  • django
  • ImageField
  • file uploads
  • file name
  • FileField
Read More

213 snippets posted so far.