Add a Save and view next button to your admin change form.
Put the code at [link](http://www.djangosnippets.org/snippets/2006/) in a file called myapp/templates/admin/myapp/change_form.html
Note: Requires Django 1.2.
See docstrings for details. To use, add to `MIDDLEWARE_CLASSES` in `settings.py`, and in your `views.py`:
1. `from path.to.this.middleware import secure`
2. Decorate SSL views with `@secure`
This is what I use to send simple status emails from my sites. Instead of a django.core.mail.send_mail call, which can take an irrritatingly, nondeterministically long time to return (regardless of error state), you can stow the emails in the database and rely on a separate interpreter process send them off (using a per-minute cron job or what have you). You then also have a record of everything you've sent via email from your code without having to configure your outbound SMTP server to store them or anything like that.
Usage notes are in the docstring; share and enjoy.
This is a ModelChoiceField where the choices are rendered in optiongroups
(this is already posible with a normal Choicefield)
For this to work properly the queryset you supply should already be ordered
the way you want (i.e. by the group_by_field first, then any sub-ordering)
See [related blog article](http://anentropic.wordpress.com/2010/03/23/django-optiongroups-for-your-modelchoice-field/)
Serves images from a local directory, but if it doesn't find it locally, then go look for it on a server. This is useful for sites with very large amounts of static content.
Instead of copying all your prod images to dev every time you update the database, simply use this in your URL patterns to serve the images:
`(r'^(?P<path>.*)$', 'static_fallback.serve', {'document_root' : '/path/to/my/files/'})`
By default, it caches the images locally in the path it was supposed to find them. The fallback server URL can be specified in urls.py or settings as FALLBACK_STATIC_URL.
Thanks to Johnny Dobbins for the idea. Based on the Nginx pass through image proxy concept.
For more info [http://menendez.com/blog/using-django-as-pass-through-image-proxy/](http://menendez.com/blog/using-django-as-pass-through-image-proxy/)
This template tag attempts to get specific GET variables from request.GET. If such variables exist, it shows them as a query string (with optional "include_ampersand" mode when it puts an "&" at the end if there is a result string, or a "?" if there's none: it is used when you need to add a new variable to the query string) or as hidden input fields ("html_form" mode).
Model field that stores serialized value of model class instance and returns deserialized model instance. Example usage:
from django.db import models
import SerializedObjectField
class A(models.Model):
object = SerializedObjectField(serialize_format='json')
class B(models.Model):
field = models.CharField(max_length=10)
b = B(field='test')
b.save()
a = A()
a.object = b
a.save()
a = A.object.get(pk=1)
a.object
<B: B object>
a.object.__dict__
{'field': 'test', 'id': 1}
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.
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.
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.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.