Django's templates don't provide much in the way of arithmetic: there is an "add" filter and that is about it. Even if sub, mult and div filters are implemented, it is difficult to chain filters while preserving some complicated expression, such as ((x+3)4-(2-y)/12.75). However, this expression can be converted into Reverse Polish Notation: x 3 + 4 * 2 y - 12.75 / - which is just a sequence of operations (push-value or apply-operator) and can be chained.
To use these filters, first create a new stack for the expression with name|stnew (pass it some locally unique value). To push a number (or template variable) onto the stack, call name|stpush:number (note that you have to tell stpush the name of the stack to push onto). To pop, call name|stpop. To perform an operation, call name|st[add,sub,mult,div,mod]:number. All numbers are integers if they look like integers, or floats otherwise (integers are turned into floats upon division if they need to be). All of these functions return the name of the stack so that they can be chained. When the calculation is finished (i.e. the answer is at the bottom of the stack) call name|stget to retrieve it.
Example (this was used to calculate an inline CSS value:
`left: {{ forloop.counter|stnew|stpush:res.stwkday|stpush:"9.35"|stmult|stpush:res.get_item_left|stpush:"2.75"|stadd|stadd|stget }}em;`
This exception is util when you want to raise an exception but want its message be shown as a message to the user, with no error 500 or 404 pages.
To use it, just append the middleware in the MIDDLEWARE_CLASSES setting and raises HttpMessage when necessary.
If you need two values for validation, you can't use clean_value_a() since it is undefined
if value_a or value_b gets cleaned first.
This little helper lets you add ValidationErrors to fields instead of \__all\__.
Related [Ticket 5335](http://code.djangoproject.com/ticket/5335).
This code sets the default sites for a sites ManyToMany property to `Site.objects.all()`, which makes sure you don't have to bother setting it for each item on a site.
This could easily be changed to `Site.objects.get_current()` to use the current site as default.
This template tag allows easy inclusion of google analytics script. If Google changes the script in the future, it remains easy to update the template tag with the the new code. This script is tested against Django 1.0 trunk Oct 9 2008.
**Readme**
After signing up for a Google Analytics account for your domain, define ANALYTICS_ID = "UA-XXXXXXX-X" in your settings.py with the supplied code.
Include {% load analytics %} at the top of your base.html and {% analytics %} tag at just before the closing 'body' tag of the base template.
Make a template to hold the analytics script.
templates/analytics/analytics_html
A rewrite of the django.contrib.formtools.FormWizard class to be session-based instead of saving state in hidden form variables.
It started out as a simple port of the standard FormWizard class, and much of the original behavior was unchanged because I wanted to limit the scope of the "rewrite". Along the way I think I made it a bit more OO, and a bit more flexible, but there are still warts.
I threw this code up here before I was completely happy with it just to take part in the discussion around [this ticket][http://code.djangoproject.com/ticket/9200 "Django Ticket #9200]. There's certainly a few things that the patch on that ticket does that I'd like to incorporate into my Wizard, whenever time allows.
A simple filter which divides an iterable (list, tupe, string, etc) in chunks, which can then be iterated over separately. A sample of the filter usage is given: a gallery template in which I needed to display images in a table, three images per row, one row for images followed by one row for their descriptions.
This dead-simple piece of middleware adds a terrific security feature to django authentication. Currently, users who's accounts are de-activated still may have a cookie and a login session. This middleware destroys that session on their next request.
Simply add this class into a middleware.py and add it to your settings.
This is a mod I made to the Django simple_tag system to let the simple_tags access comments. I plan to try and get it integrated into the trunk, so it's mainly here so (a) the people on django-developers can see it, and (b) while I'm waiting, or if it doesn't get put in the trunk, people can use it.
**Installing**
1. Open the module `django.template.__init__`, wherever that lives.
2. Scroll down to the beginning of " `class Library:` "
3. Find the simple_tag function (" `def simple_tag(self,func):` ")
4. Replace the function (even the whitespace before each line) with the code snippet.
**Usage**
1. When defining a simple tag (see the [docs](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#shortcut-for-simple-tags) for more info), have the first parameter in your function be named "`context`". Otherwise, an error will be thrown. It can accept other parameters like normal.
2. Use `register.simple_tag(my_function_name, takes_context=True)` to register your function as one that will use the context.
3. The tag's function can access the context like a dictionary - to get a value, just use `context['cheese']`, or to set one, use `context['cheese'] = 'Limberger'`. Due to the context's mutability, this will affect the context in the main template as well.
**Notes**
This code hasn't been tested in a "production environment", but I did test my modifications thoroughly, and if you don't add `takes_context=True`, `simple_tag` will behave exactly as normal. Of course, if there is a problem, make sure you leave a comment.
**Code**
Since most of the code is hacked up from other Django library functions, and to prepare for if and when it's merged into the trunk, it's released under the BSD license.
This attaches a signal to the save and delete signals for the Vote object and recalculates the score for the object and stores it in that object's vote_score attribute.
This allows you to have a list of those objects and not have to calculate in the database the vote score for each object.
I was trying to create a custom field to use the mysql encrypt() function on some data I wanted to store in the DB. initcrash on IRC pointed me to [this code](https://tracpub.yaco.es/cmsutils/browser/trunk/db/fields.py?rev=66) which I butchered as best as my little brain could. Amazingly enough I got it working (thanks to a couple answers from initcrash).
If anyone can clean this up that would be great. I'm also trying to figure out how I can a) create password reset link in the admin interface for this field without displaying the field or b) decrypt it so that the password field is pre-populated with the decrypted password. Otherwise it is submitting the encrypted string as a new password.
Anyways, I'm only a week or two into Django with no python experience so any suggestions are very welcome.
Hope this helps someone!
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.