Login

All snippets written in HTML/template

Snippet List

Auto Generate/Save Thumbnails using Template Filter (scale max_x, max_y, or both)

Couldn't get the original to work, and wanted more functionality (scale on x or y coordinates) <img src="{{ object.image.url }}" alt="original image"> <img src="{{ object.image|thumbnail:"250w" }}" alt="image resized to 250w x (calculated/scaled)h "> <img src="{{ object.image|thumbnail:"250h" }}" alt="image resized to (calculated/scaled)w x 250h h "> <img src="{{ object.image|thumbnail:"250x200" }}" alt="image resized to 250wx200h "> <img src="{{ object.image|thumbnail }}" alt="image resized to default 200w (or whatever you default it to) format"> Original http://www.djangosnippets.org/snippets/192/ Adapted http://www.djangosnippets.org/snippets/955/ Sampled From: http://batiste.dosimple.ch/blog/2007-05-13-1/ http://vaig.be/2008/05/17/stdimagefield-improved-image-field-for-django/

  • template
  • filter
  • image
  • template-filter
  • thumbnail
  • templatetags
Read More

table with n items per row using custom modulo tag

As a quick, simple way to take a list of items make a table with n items per row I added a custom template filter for modulo of an integer. To make the custom filter first create a "templatetags" directory in your application folder then add an empty file called "__init__.py" in that new directory. Finally add a file "myapp_tags.py" with the above code in it. In the template you load your custom filter with {% load pictures_tags %} and then you can make a table with n elements per row using a simple for loop and in if statement. This works because if ( for_loop.counter modulo n ) is zero ("if not" evaluates to True on zero values), then it makes a new table row. You might note that if the number of items in list is a multiple of n, there will be an empty row at the end... preventing that adds needless complexity so I left that out!

  • filters
  • modulo
  • tables
Read More

Date Picker Template Include

All you need to do is include the file you save this as in your other templates. The css and javascript is right in the file. Fully customizable. Very easy. Click a date to choose a single date. Click a second date to choose a range. A third click will act as a first click. note: you also will want to customize the django template specific variables in elements with ids: s_date e_date cal_month if you change those, it's not even really django specific, but i use this in my form for time off requests at work.

  • template
  • date
  • picker
  • include
Read More

update-django: Update your django git branches.

This is the (revamped) bash script I use to keep my git branches up-to-date with SVN to make my life a lot easier, just save it in a text file and read the instructions at the top! Hope it's useful to somebody else than me ;)

  • bash
  • merge
  • update
  • svn
  • git
Read More

google.js template for GoogleAdmin

JavaScript template for [GoogleAdmin](http://www.djangosnippets.org/snippets/1144/). Also requires the [google.html](http://www.djangosnippets.org/snippets/1145/) template. Install in `gis/admin` somewhere in your template path.

  • gis
  • google
  • map
  • gmaps
  • layer
  • openlayers
Read More

google.html template for GoogleAdmin

HTML template for [GoogleAdmin](http://www.djangosnippets.org/snippets/1144/). Also requires the [google.js](http://www.djangosnippets.org/snippets/1146/) template. Install in `gis/admin` somewhere in your template path.

  • gis
  • google
  • map
  • gmaps
  • layer
  • openlayers
Read More

List all Form Errors

You can place this code above your form and it will list out all errors in your form if there are errors. Sometimes I like listing the errors at the top of the form because I think it looks cleaner. Make sure you add error_messages={'required': 'My detailed error here'} in your view.py for your form.

  • error
  • form
Read More

Making prepopulate_from work with ForeignKeys and other sorts of choice fields

This is a fairly small bit template that, if placed in your_project_dir/templates/admin/prepopulated_fields_js.html will override the template that is normally pulled by the preopulated fields templatetag in the admin. The result is that you can successfully specify a ForeignKey or other field involving choices as a source for prepopulate_from in your admin.py. It works just as well when there are multiple fields of both the text and choice variety.

  • javascript
  • admin
  • prepopulate
Read More

Template tag: split list to n sublists

Based on [this snippet](http://www.djangosnippets.org/snippets/660/), adapted to split a list into *n* number of sublists, e.g. split a list of results into three evenly-divided sublists to enable display of the results in three columns on one page with CSS. Tag: `{% list_to_columns your_list as new_list number_of_columns %}` The split_seq solution comes from a comment by Sebastian Hempel on [this post](http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/425397). More info [here](http://herself.movielady.net/2008/07/16/split-list-to-columns-django-template-tag/).

  • template
  • list
  • template-tags
Read More

Template tag for compressed CSS & JS (GAE version)

Extension of the idea from [WuzHere example from Google IO](http://code.google.com/p/wuzhere/) about creating one compressed js or css file. Original code used not very elegant if statements. I've changed it to template tags. On production server it will also use current version id. Insert code in *cssjs.py* file in *templatetags* dir in your application and use as below (more details in docs): `<script type="text/javascript" src="/media/jsmergefile.js"></script>` **This code does not compress CSS and JS on the fly, because GAE is read-only and using Datastore is too heavy.**

  • templatetag
  • google-app-engine
Read More

Use django-admin.py instead of manage.py

A lot of people new to Django don't realize that `manage.py` is [just a wrapper](http://www.djangoproject.com/documentation/django-admin/) around the `django-admin.py` script installed with Django and isn't needed. (You may need to symlink `django-admin.py` to someplace in your system `PATH` such as `/usr/local/bin`.) The most important thing it does is to set your `PYTHONPATH` and `DJANGO_SETTINGS_MODULE` environment variables before calling `django-admin.py`. Those same settings are needed when you move your site on to a production server like Apache, so it is important to know how they work. This shell function sets those variables for you. Put it in your `.zshrc` or bash startup script. It works for both the monolithic project style and the lightweight app style of Django development [[1](http://www.pointy-stick.com/blog/2007/11/09/django-tip-developing-without-projects/)], [[2](http://www.b-list.org/weblog/2007/nov/09/projects/)]. This function isn't fancy; drop a comment if you have an improvement. Written for zsh and tested with bash 3.1.17.

  • bash
  • shell
  • zsh
Read More

make an unordered html list

This custom filter takes in a string and breaks it down by newline then makes each separate line an item in an unordered list. Note that it does not add the <ul> </ul> tags to give the user a chance to add attributes to the list. This is based of the 'linebreaks' filter built in django

  • html
  • list
Read More

78 snippets posted so far.