There's a whole range of examples online for resizing images in Django some of which are incredibly comprehensive with a wide variety of options. Here's my take on the task that serves as a simple drop in for when you don't want to include a separate app.
- Only generates the resized image when first requested.
- Handles maintaining proportions when specifying only a width or a height.
- Makes use of PIL.ImageOps.fit for cropping without reinventing the wheel.
It's often useful to be able to check the type of an object in templates. Most recently I used this to do some trickery in my forms for certain field types. These template filters allow you to match the type of an object or a field widget with a string. It returns True if it matches and False if it doesn't or there is an error.
for example:
{% if form|obj_type:'mycustomform' %}
<form class="custom" action="">
{% else %}
<form action="">
{% endif %}
{% if field|field_type:'checkboxinput' %}
<label class="cb_label">{{ field }} {{ field.label }}</label>
{% else %}
<label for="id_{{ field.name }}">{{ field.label }}</label> {{ field }}
{% endif %}
The popular [django-tagging](http://code.google.com/p/django-tagging/) app has, in its implementation and semantics, a highly usable and transparent elegance -- but then you have to call methods on a Tag instances' items collection. These classes let you inline the tag name in the chain of queryset filter methods instead.
TO USE:
### models.py
...
from tagging.fields import TagField
from tagging.models import Tag as Tag
class YourModel(models.Model):
...
yourtags = TagField()
objects = TaggedManager()
...
### and then elsewhere, something like--
...
ym = YourModel.objects.order_by("-modifydate")[0]
anotherym = YourModel.objects.get(id=7) ## distinct from ym
ym.yourtags = "tag1 tag2"
anotherym.yourtags = "tag1 othertag"
ym.save()
anotherym.save()
with_tag1 = YourModel.objects.tagged('tag1')
with_tag2 = YourModel.objects.tagged('tag2').order_by('-modifydate')
print ym in with_tag1 ## True
print anotherym in with_tag1 ## True
print ym in with_tag2 ## False
... since these are QuerySets, you can easily create unions (e.g. `with_tag1 | with_tag2` and othersuch) as you need and filter them to your hearts' content, without having to instantiate Tag all the time (which you can of course do as well).
A replacement for sending mail that takes the name of a template which exists in both text and html formats, and uses these to create a multipart email populated with the given context.
This combination of settings.py admin_reorder_tag.py and admin/base_site.html gives you the ability to define custom ordering for the apps and models in the admin app.
1. Add the setting ADMIN_REORDER to your settings.py as a tuple with each item containing a tuple of the app name and a tuple of the models within it, all defining the ordering to apply.
2. Drop the template tag code into admin_reorder_tag.py and put this into a templatetag package in one of your installed apps.
3. Drop the template code into your templates directory as admin/base_site.html
Template designers often require access to individual form fields or sets of form fields to control complex form layouts. While this is possible via looping through form fields in the template it can lead to ugly logic inside templates as well as losing the ability to use the as_* form rendering methods.
This class when mixed into a form class provides hooks for template designers to access individual fields or sets of fields based on various criteria such as field name prefix or fields appearing before or after certain fields in the form, while still being able to use the as_* form rendering methods against these fields.
I often find myself needing to create a template tag and all I'm interested in is the token and the render function. This decorator abstracts away the boilerplate code around creating the template node class which is the same each and every time.
I found [snippet #1016](http://www.djangosnippets.org/snippets/1016/) while looking for a way to add a button to the top of an admin change form, and it didn't work. I found that it used the old admin, so I updated it to django 1.1.1
This is an upgrade of snippet [1103](http://www.djangosnippets.org/snippets/1103/).
Exemplary usage:
class Blog(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Post(models.Model):
title = models.CharField(max_length=50)
blog = models.ForeignKey(Blog)
def __unicode__(self):
return self.title
class Meta:
abstract=True
class Article(Post):
text = models.TextField()
class Link(Post):
url = models.URLField()
blog = Blog(name="Exemplary blog")
blog.save()
Article(title="#1", text="Exemplary article 1", blog=blog).save()
Article(title="#2", text="Exemplary article 2", blog=blog).save()
Link(title="#3", url="http://exemplary.link.com/", blog=blog).save()
qs1 = Article.objects.all()
qs2 = Link.objects.all()
qsseq = QuerySetSequence(qs1, qs2)
# those all work also on IableSequence
len(qsseq)
len(QuerySetSequence(qs2, qs2))
qsseq[len(qs1)].title
# this is QuerySetSequence specific
qsseq.order_by('blog.name','-title')
excluded_homo = qsseq.exclude(title__contains="3")
# homogenic results - returns QuerySet
type(excluded_homo)
excluded_hetero = qsseq.exclude(title="#2")
# heterogenic results - returns QuerySetSequence
type(excluded_hetero)
excluded_hetero.exists()
You can implement more `QuerySet` API methods if needed. If full API is implemented it makes sense to also subclass the `QuerySet` class.
By using this simple wrapper instead of Django's default send_mail function, you gain the peace of mind of knowing that when settings.DEBUG == True, all the emails will be sent to you instead of the original recipient. Handy for testing.
Add this to any admin changelist and your actions drop-down will be replaced with user-friendly buttons.
Why mess around with templates and subclassing admin classes when you can just mangle the page with jQuery! ;-)
It also adds a 'select all' label to explain the mystery top check-box (well it was a mystery to several of my clients).
The line "if ($('div.actions option:gt(0)').length<=8)" checks that there aren't more than 8 actions and falls back to the drop-down if there are.
Requires jQuery to be loaded.
in this case the 'render_template' decorator assumes there is a myview.html template. this keeps things simple and you DRY. Hope it helps.
Regards,
Paul