Login

Top-rated snippets

Snippet List

Chain multiple querysets into one

This class acts as a wrapper around multiple querysets. Use it if you want to chain multiple QSs together without combining them with | or &. eg., to put title matches ahead of body matches: >>> qs1 = Event.objects.filter(## title matches ##) >>> qs2 = Event.objects.filter(## matches in other fields ##) >>> qs = MultiQuerySet(qs1, qs2) >>> len(qs) >>> paginator = Paginator(qs) >>> first_ten = qs[:10] It effectively acts as an immutable, sliceable QuerySet (with only a very limited subset of the QuerySet api)

  • queryset
  • chain
  • multi
Read More

change SITE_ID on fly

Django SITE_ID is a global setting, so the site framework requires you to run multiple instances of Django; at least one for each site. But i want have one instance for multiple sites. This snippet solve this task by change SITE_ID on fly. /sorry my bad english/

  • settings
  • site_id
  • site-contrib
Read More

Django JQuery Autocomplete for Model Selection

This is a general JQuery Autocomplete Form Field for selecting any model instance in your forms. 1 Download jquery.1.2.6 and the jquery autocomplete plugin http://bassistance.de/jquery-plugins/jquery-plugin-autocomplete/, place theme somewhere in your media directory (in this case {{ MEDIA__URL}}/js/ 2 copy fields.py to anywhere in your python-path (in this case utils.fields) 3 create a view for the ajax request that receives a query and returns a key|value list, and register it in your urls.py 4 Just Use the fields in any form like in forms.py

  • selection
  • model
  • jquery
  • autocomplete
Read More

simple email validation function

This very basic function I was missing in this part of the Django Documentation when creating a custom MultiEmailField: http://docs.djangoproject.com/en/dev/ref/forms/validation/

  • email
  • validation
Read More

Photologue wiki-syntax templatetag

**updated 12/16/08** I run several blogs by visual-artists and web-designers who want a quick way to insert images into their blog without the awkwardness of WSYIWYG and without the technicality of code (eww...). Thanks in advance for your input. #Syntax in a blog goes: [[thumb:the-image-slug]] # Gives you a thumbnail [[image:the-image-slug]] # Presents full-size-image Then of course: {% blog.post|yeagowiki %} You will also need to create some templates (see snippet). Here's a sample: <!-- /templates/photologue/image_snippet.html --> <div class="photologue-image"> <a href="{% if url %}{{ url }}{% else %}/media/{{ image.image }}{% endif %}"> <img src="{{ image.get_display_url }}" /> </a> <p class="photologue-image-caption">{{ image.caption }}</p> </div>

  • templatetag
  • markdown
  • wiki
  • photologue
Read More

Session Wizard

This a wizard tool similar to the one in django.contrib.formtools, but it uses a session. I hope to eventually get this into shape and contribute it to the formtools package, but for right now, here it is. The wizard steps are broken into 2 categories: Show Form and Submit Form Each category has it's own hooks for manipulating the process, for instance you can short-circuit a process_submit_form() and go straight to done() via a return from preprocess_submit_form(). Sorry for the lack of documentation on this. Here's an example urls.py entry : `(r'^estimate/(?P<page0>\d+)/$', EstimateWizard([EstimateCustomer, EstimateJob, EstimateRoom]))` where EstimateCustomer, Job, and Room are Form classes and EstimateWizard extends SessionFormWizard

  • session
  • form
  • wizard
Read More

FormHandler - take the legwork out of form processing

Take the legwork out of processing forms. Most people have a very specific structure to how they process forms in views. If the request method is "GET", then some HTML (with the blank form) is rendered. If the method is "POST", then the form is validated. If the form is invalid, some other HTML is displayed. If valid, the data is submitted and processed in some way. In order to do this all in a much nicer way, simply subclass `FormHandler`, define three methods (`valid`, `invalid` and `unbound`), point to the form, and use the subclass as your view in the URLconf.

  • views
  • forms
  • view
  • form
  • view-as-a-class
  • formhandler
  • form-handler
Read More

Link Media Command

A command for manage.py which scans through installed applications the way admin.autodiscover does, just looking for media folders. It then creates a symbolic link to the media under MEDIA_ROOT/app_name. Usage: save in an apps management/commands folder and run with "python manage.py linkmedia" Only works on *nix systems, but there should be an equivilent way to do this with windows using .lnk files.

  • manage.py
  • media
  • command
  • symlink
Read More

Email Attachment

Django documentation is lacking in giving an example for sending an email with attachment. Hopefully this code helps those who are trying to send email with attachments

  • email
  • attachment
  • fileinput
Read More
Author: sri
  • 2
  • 12

Frequently used tags/filters for Jinja2

Some frequently used filters and global functions: **url** - same as django url tag **nbspize** - replace all spaces with nbsp **get_lang** - get current language code **timesince** - converted django timesince tag **timeuntil** - converted django timeuntil tag **truncate** - tag that truncates text call it with an str argument like '20c' or '5w', where the number provides the count and c stands for charachters and w stands for words

  • tags
  • jinja
  • jinja2
Read More

whitespaceoptimize block tag

This is a custom block tag and is used like this: {% load whitespaceoptimize %} {% whitespaceoptimize "css" %} /* CSS comment */ body { color: #CCCCCC; } {% endwhitespaceoptimize %} And when rendered you get this output: body{color:#CCC} To install it, download the snippet and call it `myapp/templatetags/whitespaceoptimize.py`. (Make sure you have a `__init__.py` in the `templatetags` directory) You need to download and install [slimmer](http://www.issuetrackerproduct.com/Download#slimmer) and put on your Python path. The block tag can be used for `javascript` and `html` as well as `css`. You can also let it guess the format; this would work for example: {% whitespaceoptimize %} <table> <tr> ... {% endwhitespaceoptimize %} ...but this would just replicate the functonality of the [built-in spaceless tag](http://docs.djangoproject.com/en/dev/ref/templates/builtins/#spaceless)

  • tag
  • block
  • whitespace
  • slimmer
Read More

FreeBSD rc.d FastCGI Script

This is a simple rc script, suitable for starting a FastCGI daemon on FreeBSD. Simply copy this into /usr/local/etc/rc.d , change all references to "signet" to the name of your app, mark it executable and modify /etc/rc.conf accordingly.

  • fcgi
  • freebsd
  • fastcgi
  • rc.d
  • rc
Read More

autotranslate po files using google translate

Save to autotranslate.py, run using python autotranslate.py pofile inputlang outputlang, eg. python autotranslate.py path_to_blank_fr_lang.po en fr, to translate to french. Some known bugs: * Doesn't handle some line returns properly * Block translations aren't formated correctly in the translation. If anyone has any issues or fixes please post to the comments. Of course the output shouldn't be used as substitute to a proper translation.

  • translation
Read More

Integer Currency Input

This accepts values such as $1,000,000 and stores them to the database as integers. It also re-renders them to the screen using the django.contrib.humanize.intcomma method which takes 1000000 and turns it into 1,000,000. Useful for large currency fields where the decimals aren't really necessary.

  • newforms
  • currency
  • field
  • integer
  • input
Read More

models with order (+admin editing)

Use this abstract model if you want to add "order" to a given model. Once you do, you will get automatic "up" and "down" links for each model row. One problem is that if the user sorts by another row, the up and down links are still there, but now don't make a lot of sense.

  • models
  • admin
  • order
Read More

3110 snippets posted so far.