This is a [Paginator Tag](http://www.djangosnippets.org/snippets/73/) for 1.x. Since the context is less overfull, the template, paginator.html, needs more logic.
Put the tag in your templatetags and the template at the root of a template-directory.
The tag will work out of the box in a generic view, other views must provide `is_paginated` set to True, `page_obj`, and `paginator`. You can get the `object_list` from the `page_obj`: `page_obj.object_list`. See [the pagination documentation](http://docs.djangoproject.com/en/1.0/topics/pagination/).
I didn't really like the current state of iPhone/Mobile redirect middleware mainly because I wanted something that was closer to twitters use case. So I came up with this. I don't think it a great snippet and I will probably fix it in the near future. But it works.
There are probably ways to improve the implementation, but this was something I came up with when I wanted to change the default size of all of my CharField admin fields. Now all I have to do in my ModelAdmin class is:
form = get_admin_form(model)
or subclass BaseAdminForm if I need extra validation or more widget customization for an individual admin form.
This is an extension of the snippet http://www.djangosnippets.org/snippets/1302/ to make it a bit more flexible and been able pass more than one parameter to our "Partial Template". To use it you can
{% partial_template template_name param1:variable1 param2:variable2 ... %}
or:
{% partial_template partials/mini_template.html item:data1 var2:"False" var3:"2*2" %}
When executing custom sql, the temptation is to use fetchall or fetchone, since the API for fetchmany is a bit awkward. (fetchall makes all records resident in client memory at once; fetchone takes a network round-trip to the DB for each record.)
This snippet, hoisted from django.db.models.sql.query.results_iter, presents a nice, simple iterator over multiple fetchmany calls which hits a sweet spot of minimizing memory and network usage.
Add this as a superclass of any Django model to allow making copies of instances of that model:
class Entry(models.Model, CloneableMixin):
[...]
e = Entry.objects.get(...)
e_clone = e.clone()
e_clone.title = 'Cloned Entry'
e.save()
The new object is saved during the clone process and ManyToMany relations are copied as well.
This patch adds a new admin Filter, for Filtering nullable fields. It adds 3 possible choices: 'All' (no filter), 'Null' (it applies field__isnull=True filter), and 'With Value' (it filters null values).
This patch is interesting when you have a Integer or String fields and you want to filter wether a value is set or not. In other case, it would show too many filtering options.
Remember this is a patch and you must modify a django file in `django/contrib/admin/filterspecs.py`
Invoice numbers like "0000004" are a little unprofessional in that they expose how many sales a system has made, and can be used to monitor the rate of sales over a given time. They are also harder for customers to read back to you, especially if they are 10 digits long.
This is simply a perfect hash function to convert an integer (from eg an ID AutoField) to a unique number. The ID is then made shorter and more user-friendly by converting to a string of letters and numbers that wont be confused for one another (in speech or text).
To use it:
import friendly_id
class MyModel(models.Model):
invoice_id = models.CharField(max_length=6, null=True, blank=True, unique=True)
def save(self, *args, **kwargs):
super(MyModel, self).save(*args, **kwargs)
# Populate the invoice_id if it is missing
if self.id and not self.invoice_id:
self.invoice_id = friendly_id.encode(self.id)
super(MyModel, self).save(*args, **kwargs)
if self.id and not self.invoice_id
When an object from this model is saved, an invoice ID will be generated that does not resemble those surrounding it. For example, where you are expecting millions of invoices the IDs generated from the AutoField primary key will be:
obj.id obj.invoice_id
1 TTH9R
2 45FLU
3 6ACXD
4 8G98W
5 AQ6HF
6 DV3TY
...
9999999 J8UE5
The functions are deterministic, so running it again sometime will give the same result, and generated strings are unique for the given range (the default max is 10,000,000). Specifying a higher range allows you to have more IDs, but all the strings will then be longer. You have to decide which you need: short strings or many strings :-)
This problem could have also been solved using a random invoice_id generator, but that might cause collisions which cost time to rectify, especially when a decent proportion of the available values are taken (eg 10%). Anyhow, someone else has now already written this little module for you, so now you don't have to write your own :-)
Simply returns a created gravatar url based on input. Creates the url utilizing the full gravatar url inputs as defined at http://en.gravatar.com/site/implement/url.
Jinja2, while a great replacement for Django templates, is not a drop-in replacement for it. I wanted to use Photologue with my Jinja templates, but because Photologue uses Django generics, so I decided to see if I could use Jinja2 with generics, and then only modify the templates. It was a bit of work, but I seem to have done it. Django generics can take template_loader as an option, so if you have the same interface, things should just work.
The template must accept RequestContext as an argument to render(), so here we subclass jinja2.Template and when it receives Django's RequestContext object, it creates a flat dictionary from it, which jinja2 can work with.
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!
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.