Login

Most bookmarked snippets

Snippet List

Update All Apps to Latest Revision

A simple script that I have put in my Django applications directory to fetch the latest application code from git and svn. For example, your directory structure might look like so: django-apps/ django-tagging/ django-pagination/ django-registration/ django-threadedcomments/ django-mptt/ update_apps.py Where update_apps.py is the source of this snippet. To run, simply execute: # python update_apps.py And the script will iterate through all of your apps and update them to the latest version.

  • script
  • update
  • apps
  • project
  • svn
  • git
Read More

Read more link

I couldn't find any code for a blog-style "Read more after the jump," so I made a custom filter. It will look for **<!--more-->** for the jump, like in Wordpress. In **settings.py** set **READ_MORE_TEXT** to what you want the text of the link to be. `READ_MORE_TEXT = 'Read more after the jump.'` When you call the filter in your template, pass it the absolute link of that entry. Of course, you have to have your **get_absolute_url** function defined in your model, but you should have that already, right? :P In this example **entry.body** is the content of the blog entry. `{% load blog_filters %}` `{{ entry.body|read_more:entry.get_absolute_url }}` If anyone has a better way to do this, it is, of course, welcome.

  • template
  • filter
  • blog
  • find
  • jump
  • read
  • more
Read More

Google Charts Templatetags (Python)

The core templatetags for my project [google-chartwrapper](http://code.google.com/p/google-chartwrapper/). It is an easy method of creating dynamic GoogleCharts from the [GoogleChartAPI](http://code.google.com/apis/chart/). To get the most recent version: `svn checkout http://google-chartwrapper.googlecode.com/svn/trunk/` and run `python setup.py` in the downloaded trunk directory. There is an included django project there with the [ChartsExamples](http://code.google.com/p/google-chartwrapper/wiki/ChartExamples) all worked out in django templates

  • templatetags
  • google
  • chart
Read More

EditInline for GenericForiegnKey II

This is an update to [snippet 765](http://www.djangosnippets.org/snippets/765/) as I was having trouble getting it to work on branches/newforms-admin @ r7771. There are just a few minor changes to the previous snippet, all simple stuff. I went ahead an added can_delete and can_order options that the previous snippet didn't include. [More details in blog post](http://paltman.com/2008/06/29/edit-inline-support-for-generic-relations/).

  • admin
  • foreignkey
  • generic
  • edit-inline
Read More

Gravatar Images

This is a simple templatetag for including [Gravatars](http://www.gravatar.com/) on your Django site. Usage is `{% gravatar some_user %}` or `{% gravatar some_user 40 %}`

  • avatar
  • gravatar
Read More

DisplayModelForm

Subclass of the ModelForm which allows to make fields 'display_only'. This means no formfield will be displayed, but a suitable representation. You can make all fields display_only or just a few (or you can use the form as a normal modelform). There are also some extra's to easily set attrs on fields or set help_texts on widgets when using this form. Why ? I made my own set of generic crud views based on newforms, but added a 'display' view to simply display objects, in the same table layout as the editing is done, but without the fields. I wanted to avoid having to redefine my forms twice and I wanted to reuse some generic templates as much as possible. Obviously this is not good for performance, but I use it for an intranet app with a lot of objects, but not that big a load. Their is definitely still a lot of room for improvement, and maybe all this could have been done much easier. How to use? See the docstring.

  • modelform
Read More

Remove self links middleware

This simple middleware replaces all 'a href' links to the current page to the 'span' elements. This very usefule from the usability point of view. For example, user open in bowser page http://svetlyak.ru/blog/, and this middleware will replace all 'a' elements on this page, which refer to the '/blog/'. Because of this, link 'Blog' in the main menu, become a simple 'span'. Next, when user goes to the next page, a post with full comments list ('/blog/123/'), for example, the item 'Blog' in the main menu become a link again! To use this middleware, just add it to the list of middleware classes: MIDDLEWARE_CLASSES = ('utils.middleware.RemoveSelfLinks',)

  • middleware
  • html
  • output
  • usability
  • utils
Read More

Resize image on save

This snippet is extracted from my Photo model. I use it to ensure that any uploaded image is constrained to a specified size (resized on save). In my case, I don't need to maintain a "thumbnail" and "fullsize" version, so I just store the resized version to save space.

  • image
  • pil
  • resize
Read More

Searchable Media

This app allows you to utilize mysql's fulltext searching over multiple models and multiple apps, letting the site search seem more intuitive, yet still allow your content to be very structured. Essentially, it creates an entire new model that associates objects to a chunk of text to search efficiently over (and index), using the contenttypes app. Simply add the post_save events to your existing models for things you want to be searched.

  • search
  • media
  • fulltext
  • mixed
Read More

chdjango

This is a convenient script for those working with different branches of Django. Place all of your branches in `~/django` (e.g., `~/django/newforms-admin`, `~/django/trunk`) and you're ready to quickly change between them. For example: `chdjango.py trunk`.

  • django
  • path
  • change
  • chdjango
  • branches
  • pth
  • site-packages
Read More

Automagically import settings from installed applications

Use this snippet at the end of your main settings.py file to automagically import the settings defined in each app of `INSTALLED_APPS` that begins with `APPS_BASE_NAME`. Set `APPS_BASE_NAME` to the base name of your Django project (e.g. the parent directory) and put `settings.py` files in every app directory (next to the `models.py` file) you want to have local settings in. # works in the Django shell >>> from django.conf import settings >>> settings.TEST_SETTING_FROM_APP "this is great for reusable apps" Please keep in mind that the imported settings will overwrite the already given and preceding settings, e.g. when you use the same setting name in different applications. Props to [bartTC](http://www.djangosnippets.org/users/bartTC/) for the idea.

  • settings
  • import
  • reusable
  • apps
  • applications
Read More

Auto rendering decorator with options

This view decorator renders automaticaly the template with the context provided both by the view "return" statement. For example: @auto_render def my_view(request): ... return 'base.html', locals() You can still return HttpResponse and HttpResponseRedirect objects without any problems. If you use Ajax requests, this decorator is even more useful. Imagine this layout: def aggregating_view(request): ... context = locals() partial1 = partial_view_1(request, only_context=True) partial2 = partial_view_2(request, only_context=True) # aggregate template include partial templates return 'aggregate.htmt', context.update(partial1).update(partial2) def partial_view_1(request): ... return 'partial_1.html', locals() def partial_view_2(request): ... return 'partial_2.html', locals() This way you can render you view individualy for specific ajax calls and also get their context for the aggregating view.

  • render_to_response
  • ajax
  • decorator
  • rendering
Read More

Currency filter

Formats a number in the local currency format. E.g., if `foo` is equal to `49277`, then > ` {{ foo|currency }}` would print > `$49,277` If your locale is the U.S. You can use this filter in your templates as described in the [Django documentation](http://www.djangoproject.com/documentation/templates_python/)

  • filter
  • templatetags
  • money
  • currency
  • dollars
Read More

3110 snippets posted so far.