This code provides a primary key field that is globally unique. It uses the pre_save method to auto-populate the field with a Universal Unique Id as the record is saved the first time.
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.
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.
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/
simple search with Q object you just pass a list of fields and the search string then it will come up with a object to use in a filter. This snippet is thanks to Julien Phalip
at [julienphalip.com](http://www.julienphalip.com/blog/2008/08/16/adding-search-django-site-snap/)
Sometimes it's a real pain to use the `@login_required` decorator all over the views of a complicated site. This middleware requires login on every page by default and supports a list of regular expression to figure out the exceptions. This way you don't have to worry about forgetting to decorate a view.
This snippet requires `LOGIN_URL` to be set in settings.py, and optionally allows you fill out `LOGIN_EXEMPT_URLS`, a tuple of regular expressions (similar to urls.py) that lists your exceptions.
Example:
``
LOGIN_EXEMPT_URLS = (
r'^about\.html$',
r'^legal/', # allow the entire /legal/* subsection
)
``
This middleware redirects the request for yoursite.com/feed/whatever/onefeed to your feedburner *onefeed* feed.
Having
``FEEDBURNER = ('SomeName', ('blog', 'comments', 'tag1'))``
will use the feedburner feeds at
http://feedproxy.google.com/SomeName/blog
http://feedproxy.google.com/SomeName/comments
http://feedproxy.google.com/SomeName/tag/tag1
you can add more tags, or even intersection and union of them the same way
(thanks to piranha for the idea of a middleware)
**Update:** now it works for tags as well
This code will allow you to use chained select boxes in the django automatic admin area. For example, you may have a product, then a category and subcategory. You'd like to create a product, and then choose a category, and then have a chained select box be filled with the appropriate subcategories.
Move Items up and down from the admin interface. Like phpBB does it with its forums.
An additional select field is added to the admin form. After the model has been saved, a model method is called (with the value of the new field), which handles the reordering.
A more detailed description and a screenshot can be found [here](http://blog.vicox.net/2008/09/04/ordering-in-django-10/).
See the description in the blog entry at [http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/](http://sciyoshi.com/blog/2008/aug/27/using-akismet-djangos-new-comments-framework/)
Sometimes it's useful to sign data to ensure the user does not tamper with it - for example, cookies or hidden form variables. SHA1 is cryptographically secure but weighs in at 40 characters, which is pretty long if you're going to be passing the data around in a URL or a cookie.
These functions knock an SHA1 hash down to just 27 characters, thanks to a base65 encoding that only uses URL-safe characters (defined as characters which are unmodified by Python's urllib.urlencode function). This compressed hash can then be passed around in cookies or URLs, and uncompressed again when the signature needs to be checked.
UPDATE: You probably shouldn't use this; see [http://fi.am/entry/urlsafe-base64-encodingdecoding-in-two-lines/](http://fi.am/entry/urlsafe-base64-encodingdecoding-in-two-lines/) for a smarter approach based on Python's built-in base64 module.
A `models.Manager` subclass that helps to remove some of the boilerplate involved in creating managers from certain queries. Usually, a manager would be created by doing this:
class MyManager(models.Manager):
def get_query_set(self):
return super(MyManager, self).get_query_set().filter(query=blah)
Other managers may return other query sets, but this is especially useful as one may define queries on a table which would be used a lot. Since the only part that ever changes is the `query=blah` set of keyword arguments, I decided to abstract that into a class which, besides taking the repetition out of manager definition, allows them to be and'd and or'd in a manner similar to the `Q` objects used for complex database queries.
`CustomQueryManager` instances may be defined in one of two ways. The first, more laborious but reusable manner, is to subclass it, like so:
class MyManager(CustomQueryManager):
query = Q(some=query)
Then, `MyManager` is instantiated with no arguments on a model, like normal managers. This allows a query to be reused without extra typing and copying, and keeps code DRY.
Another way to do this is to pass a `Q` object to the `__init__` method of the `CustomQueryManager` class itself, on the model. This would be done like so:
class MyModel(models.Model):
field1 = models.CharField(maxlength=100)
field2 = models.PositiveIntegerField()
my_mgr = CustomQueryManager(Q(field1='Hello, World'))
This should mainly be used when a query is only used once, on a particular model. Either way, the definition of `__and__` and `__or__` methods on the `CustomQueryManager` class allow the use of the `&` and `|` operators on instances of the manager and on queries. For example:
class Booking(models.Model):
start_date = models.DateField()
end_date = models.DateField()
public = models.BooleanField()
confirmed = models.BooleanField()
public_bookings = CustomQueryManager(Q(public=True))
private_bookings = public_bookings.not_()
confirmed_bookings = CustomQueryManager(Q(confirmed=True))
public_confirmed = public_bookings & confirmed_bookings
public_unconfirmed = public_bookings & confirmed_bookings.not_()
public_or_confirmed = public_bookings | confirmed_bookings
public_past = public_bookings & Q(end_date__lt=models.LazyDate())
public_present = public_bookings & Q(start_date__lte=models.LazyDate(), end_date__gte=models.LazyDate())
public_future = public_bookings & Q(start_date__gt=models.LazyDate())
As you can see, `CustomQueryManager` instances can be manipulated much like `Q` objects, including combination, via `&` (and) and `|` (or), with other managers (currently only other `CustomQueryManager` instances) and even `Q` objects. This makes it easy to define a set of prepared queries on the set of data represented by a model, and removes a lot of the boilerplate of usual manager definition.
Another `JsonResponse` class, including comment wrapping. Extensions to other kinds of CSRF protection should be obvious. Good explanations of why such protections are needed would make excellent comments on this snippet.
This depends on the `json_encode` method in [snippet 800](http://www.djangosnippets.org/snippets/800/).
This templatetag obsfucate mailto link and hide it while not happen onmouseover event.
Usage:
{% hide_email object.author object.author.email %}
Output:
<a href="mailto:[email protected]"
onmouseover="var a=String.fromCharCode(79+35,14+103,66+41,30+71,49+49,16+81);
var b=String.fromCharCode(14+50,9+105,61+56,81+26,92+9,2+96,20+77,13+33,75+24,32+79,31+78);
this.href=['mail','to:',a,b].join('');">rukeba</a>
Based on [Email Obsfucator](http://www.djangosnippets.org/snippets/536/) and [forums at Yandex.Market](http://market.yandex.ru/forums/).
Example at [my guitar blog](http://rukeba.com/ra/2008/01/15/oasis-wonderwall/)
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.