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 :-)
As I was unable to find good examples to render a Form with two or more inlineformsets.
Therefor I have posted this to Django snippets.
This code is little different from another snippet with a Form with one InlineFormSet (the prefixes are necessary in this situation).
The example shows a person's data together with two inline formsets (phonenumbers and addresses) for a person.
You can add, update and delete from this form.
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.
As I was unable to find good examples to render an inlineformset together, I have posted this to Django snippets.
The example shows a person's data together with the phonenumbers for that person.
You can add, update and delete from this form.
The utility of a login script is self-evident. As I learned about Django's built-in user authentication features, I whipped up this script and figured that I'd post it here. I am by no means an expert and would appreciate any constructive criticism. However, per the rules of this site, this is working code and not work in progress.
Thanks
Also: I wrote a blog post explaining the script for those who are interested: http://bit.ly/bwIL
Allows to create bigint (mysql), bigserial (psql), or NUMBER(19) (oracle) fields which have auto-increment set by using the AutoField of django, therefore ensuring that the ID gets updated in the instance when calling its 'save()' method.
If you would only subclass IntegerField to BigIntegerField and use that as your primary key, your model instance you create would not get the id attribute set when calling 'save()', buy instead you would have to query and load the instance from the DB again to get the ID.
This template tag takes the current GET query, and modifies or adds the value you specify. This is great for GET-query-driven views, where you want to provide URLs which reconfigure the view somehow.
**Example Usage:**
`{% get_string "sort_by" "date" %}` returns `all=your¤t=get&variables=plus&sort_by=date`
This filter can be used to wrap <span class='highlight'> around anything you want to highlight in a block of text. For example, if you had 'foo bar foo baz' inside the context variable MYTEXT, you could do {{ MYTEXT|highlight:'foo' }}, and "<span class='highlight'>foo</span> bar <span class='highlight'>foo</span> baz" would be returned.
How you style the highlight class is up to you.
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.
This is exacly the same snippet as #197 http://www.djangosnippets.org/snippets/197/ but returning search enigne, search engine domain and search term in:
request.search_referrer_engine
request.search_referrer_domain
request.search_referrer_term
I wanted to show ads only to people comming from search engines so I took snippet #197 and modify it to put that info in the request object.
Use this code in *change_form.html* in your projects admin templates to add a character counter beside the input field(s) in admin to let users know how many characters they have remaining for a particular input field. The total number of characters allowed is determined by the max_length in your model for the models.CharField you're using this with.
This code is designed to add the counter after the input field, but could easily be customized to fit the style of any admin. If the number of characters remaining is 10 or less the background color changes to yellow to visually warn the user.
**Usage Examples:**
In this example only the input field with id=id_pull_quote will receive the counter:
$(document).ready(function() {
$("#id_pull_quote").counter();
});
You could also apply the counter to all input fields on a page:
$(document).ready(function() {
$("form input[@maxlength]").counter();
});
**Note:** *You have to download jQuery to your project and place the appropriate call in order for this to work. The best place to do this is in the extrahead block. I left my call in as an example but your path and file name will probably vary.*
Credit for base jQuery code goes to Brad Landis at [bradlis7.com](http://www.bradlis7.com).
Couldn't get the original to work, and wanted more functionality (scale on x or y coordinates)
<img src="{{ object.image.url }}" alt="original image">
<img src="{{ object.image|thumbnail:"250w" }}" alt="image resized to 250w x (calculated/scaled)h ">
<img src="{{ object.image|thumbnail:"250h" }}" alt="image resized to (calculated/scaled)w x 250h h ">
<img src="{{ object.image|thumbnail:"250x200" }}" alt="image resized to 250wx200h ">
<img src="{{ object.image|thumbnail }}" alt="image resized to default 200w (or whatever you default it to) format">
Original http://www.djangosnippets.org/snippets/192/
Adapted http://www.djangosnippets.org/snippets/955/
Sampled From:
http://batiste.dosimple.ch/blog/2007-05-13-1/
http://vaig.be/2008/05/17/stdimagefield-improved-image-field-for-django/
A simple view used to manage the page cache stored in the database (here Postgresql in the django_cache table, you also have to set correctly CACHE_TABLE_OID, by the OID of the cache table (you can get it in PgAdmin)
This middleware will put the sessionid in every place that it might be needed, I mean, as a hidden input in every form, at the end of the document as a javascrit variable to allow the AJAX request use it and of course as a GET variable of the request.
To make it work correctly the MIDDLEWARE_CLASSES tuple must be in this order:
`
'CookielessSessionPreMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'CookielessSessionPosMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
`
Hope it work for someone else out there.