Login

Most bookmarked snippets

Snippet List

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

Translated choices fields

- Choices are saved as the key integers. - Admin will show the correct translation in forms. - You can reuse the make_choices function for other choices fields. - Bad side: bin/make_messages.py won't get the choices values automatically, you have to add them in the .po's by hand.

  • models
  • choices
  • i18n
Read More

Parse TemplateTag Variables Safely

Often times I want to be able to interchangeably pass either literal strings or context variables to a custom templatetag. This function first checks a string for the existence of surrounding quotes and uses the string inside. If the string didn't explicitly use quotes it checks to see if it can resolve the string as a context variable. As a fallback the function will simply return the full string. Example: {% get_latest_by_tag 'django' %} {% get_latest_by_tag object.tag %}

  • templatetag
Read More

activation_required

Simple decorator that checks for authentication and activation of account and redirect to login or activation page if needed Your ulrsconf file must have named urls with parameters that you call that decorator Dont forget to import reverse function from django.core.urlresolvers import reverse

  • decorator
  • auth
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

Default settings for a app

Save this as conf.py in the app's directory. Now you can do `from myapp.conf import settings`. You can access from the imported object every item of your settings.py including the default settings of myapp. Though you don't have to define every setting in settings.py you use in your app. Now you can ommit annoying try...except statements to define defaults directly in the code.

  • settings
  • conf
  • app
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

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

AlphabeticFilterSpec

**WARNING: a better version of this snippet you can see at [http://www.djangosnippets.org/snippets/1051/](http://www.djangosnippets.org/snippets/1051/)** This filter spec is util only for who uses newforms-admin branch. To use this, you need to extend the class ModelAdmin for your model class, like the MyClassAdmin class in the code, with attention to the following lines: # Appends the filter cl.filter_specs.insert(0, AlphabeticFilterSpec(cl.lookup_opts.get_field('name'),request,cl.params,self.model,self))

  • filter
  • admin
  • alphabetic
Read More

Generating Taconite command documents

Hello, This is a port of a php class used to generate XML taconite command documents, useful for (very) easy and powerful ajaxy stuff, if you don't know what that is just check it there in french : http://www.desfrenes.com/playground/taconite/ or there in english : http://www.malsup.com/jquery/taconite/. Basically what it does is generate an XML document that is later processed by a javascript plugin which executes a serie of DOM modifications. About the code, I'm a Django beginner as well as a Python beginner so kind advices are welcome. Cheers.

  • ajax
  • django
  • javascript
  • python
  • jquery
  • taconite
  • minidom
  • dom
Read More

Alter Column Lengths of Contrib Apps

If you want to modify the length of a column of a contrib application, you can either modify django (cumbersome) or you can run a post_syncdb signal hook. Related: [Ticket 4748](http://code.djangoproject.com/ticket/4748)

  • signals
Read More

Convert XHTML-compatible shorttag to HTML-compatible tag.

This filter converts a XHTML-compatible shorttag `<input ... />` to a HTML4-compatible tag `<input ...>`. Example: `{% for field in form %} <dt>{{ field.label_tag }}</dt> <dd> {{ field.errors }} {{ field|remove_shorttag }} </dd> {% endfor %}` This will produce html4-compatible output, opposed to newform's normal XHTML output.

  • newforms
  • html
  • xhtml
  • html4
  • shorttag
Read More

XhtmlMortifierMiddleware

This middleware checks for xhtml mimetypes if the browser supports a "application/xhtml+xml" response. If not, it converts the response headers to "text/html". To enable this middleware add it the the MIDDLEWARE_CLASSES setting and make sure it appears somewhere after GZipMiddleware, so that it's processed first.

  • middleware
  • xhtml
  • user-agent
Read More

3110 snippets posted so far.