Login

All snippets written in Python

2956 snippets

Snippet List

Movable Type Import

This is a quick and dirty way to import blog entries and comments from Movable Type. Sorry I don't have time to document it better. Hopefully it will help you get started if you're looking to move from MT to Django.

  • type
  • import
  • movable-type
  • movable
  • mt
Read More

Link filter

Almost too dumb to be worth posting but its saved me a lot of typing. Instead of: <div> <a href="{{ object.get_absolute_url }} class="object-icon">{{ object }}</a> </div> just put: <div>{{ object|link }}</div>

  • filter
  • links
Read More

lazy url reverse()

Since the decorators of your views are evaluated during parsing urls.py you have an 'chicken - egg' problem. The method reverse() can't be used since urls.py is not read. This snippets evaluates reverse() lazy. [Related ticket: 5925](http://code.djangoproject.com/ticket/5925) Django 1.4 (current trunk) has a lazy reverse.

  • decorator
  • reverse
  • lazy
Read More

CountryField

**How to use** put this code somewhere you want, import CountryField and use

  • field
  • country
  • countries
Read More

AutoSlugField

This is a simple solution aimed at saving some time when you simply want to get a slug from a model field just before saving.

  • fields
  • slug
  • field
Read More

Another means of updating a subset of a model's fields

Based on the UPDATE query section of `Model.save()`, this is another means of limiting the fields which are used in an UPDATE statement and bypassing the check for object existence which is made when you use `Model.save()`. Just make whatever changes you want to your model instance and call `update`, passing your instance and the names of any fields to be updated. Usage example: import datetime from forum.models import Topic from forum.utils.models import update topic = Topic.objects.get(pk=1) topic.post_count += 1 topic.last_post_at = datetime.datetime.now() update(topic, 'post_count', 'last_post_at') (Originally intended as a comment on [Snippet 479](/snippets/479/), but comments aren't working for me)

  • model
  • db
  • update
Read More

Select multiple using a manytomany checkbox

Usage: from yourapp.fields import CheckBoxManyToMany from django.db import models class Weekday(models.Model): name = models.CharField() def __unicode__(self): return self.name class EventWithWeekday(models.Model): weekdays = CheckBoxManyToMany(Weekday)

  • checkbox
  • multiple
  • oldforms
  • select
Read More

FileField / ImageField with a delete checkbox

Example model: class MyModel(models.Model): file = RemovableFileField(upload_to='files', \ null=True, blank=True) image = RemovableImageField(upload_to='images', \ null=True, blank=True) A delete checkbox will be automatically rendered when editing such a model using form_for_instance. [UPDATED version which works with ModelForms](http://www.djangosnippets.org/snippets/636/)

  • newforms
  • models
  • imagefield
  • filefield
  • remove
  • delete
Read More

HTML to POST dict converter

If you want to test a post request to a form, you need to give all input fields, even if you just want to test if one value gets changed. This snippets parses the HTML of a view and gives you a dictionary that you can give to django.test.Client.

  • testing
Read More

Template filters utils

**Explanation:** That template filters is util for combine boolean-return conditions using template tag `{% if %}`. **Setup:** Insert the snippet into an_app/templatetags/myutils.py. **Use in template:** {% load myutils %} and use filters as following: - `{% if 10|is_multiple_of:5 %}` - `{% if 10|in_list:a_list_variable %}` - `{% if user.username|is_equal:a_variable %}` - `{% if user.username|is_not_equal:a_variable %}` - `{% if a_variable_|is_lt:5 %}` - `{% if a_variable_|is_lte:5 %}` - `{% if a_variable_|is_gt:5 %}` - `{% if a_variable_|is_gte:5 %}`

  • template-filters
Read More

Mako support as decorator

This snippet allows you to use @mako("templatename.mako") to define a mako template to use. Your python method just has to return a dictionary that will be passed to mako's context, after the addition of the "request" variable to the dictionary. If you return a non-False non dictionary object, it will return that instead of trying to render the template. The **request** variable is automatically added to the template's context. It also searches the current application's mako_templates/ subdirectory first before searching under the current directories mako_templates/, and does not use other application's mako_templates/ subdirectories at all. On Mako exceptions, it will display the Mako error page when debugging is enabled (hoping for extensible exception handling in the future to keep consistency) I got the idea from a [CherryPy tool](http://tools.cherrypy.org/wiki/Mako).

  • mako
Read More
Author: rmt
  • 2
  • 2

Copy/Paste form generation

Generator to help make newforms classes a bit like inspectdb does for generating rough model code. This is a standalone script to go in your project directory with settings.py and called from the prompt. It looks up the model, and generates code to copy/paste into your app. Hopefully to make a building a custom form with a lot of fields a little easier. Every argument should be a model identifier of form appname.modelname. Usage from the command line: python form_gen myapp.MyModel myapp.MyOtherModel

  • form
  • copy
  • generator
  • paste
Read More

ingore_fields

I use this snippet when creating newforms with form_for_model and form_for_instance. Both do not have an argument that lets you filter out fields easily so I wrote this instead. Typical use: `form_class = form_for_instance(obj, fields=ignore_fields(model_class, ['field_name1']))`

  • newforms
  • fields
  • ignore
Read More