A simple login form that does the actual authentification itself.
**Usage:**
if request.method == "POST":
loginform = LoginForm(request.POST)
if loginform.login():
return HttpResponseRedirect(redir_url)
else:
loginform = LoginForm()
You can use custom SQL statements with the existing database API by creating a subquery specified via the `tables` parameter of `extra`. (This has only been tested with MySQL and may not work with all database back-ends.)
An ORM model for the Sphinx full-text search engine.
See http://www.sphinxsearch.com/ for more information.
It currently supports the following:
class MyModel(models.Model):
search = SphinxSearch()
MyModel.search.query('query')
MyModel.search.query('query').order_by('@weight', '@id', 'my_attribute')
MyModel.search.query('query').filter(my_attribute=5)
MyModel.search.query('query').filter(my_other_attribute=[5, 3,4])
MyModel.search.query('query').exclude(my_attribute=5)[0:10]
MyModel.search.query('query').count()
SphinxSearch().query('hello').on_index('model_myapp model_myotherapp')
Returns an ordered list of the objects in your database.
-- Update:
New Methods:
* count()
* index_on(<str index>)
* extra(<see django>)
* all() (does nothing)
* select_related(<see django>)
* group_by(<str attribute>, <const function>[, <str sort>)
* weights(<list weights>)
This snippet is based on djangos urlize filter. It converts http:// links to youtube into youtube-embed statements, so that one can provide a simple link to a youtube video and this filter will embed it. I used it for a fun blog app.
A slightly improved version of [snippet #195](/snippets/195/) which keeps the logic but makes use of the `simple_tag` decorator to drastically simplify the code.
For an alternative to this sort of tag, check out the media context processor in my [template_utils app](http://code.google.com/p/django-template-utils/).
I'm posting this here mostly because I need a more permanent home for this than my lappy's hard drive. I hope it's interesting to other people, though.
Anyway - this script is what I use to test Django against multiple versions of Python and multiple databases. To actually run this you'll of course need Python 2.3, 2.4, and 2.5 installed along with MySQL, PostgreSQL, and sqlite3 -- and the associated database wrappers for all 3 Pythons.
Yes, for the record, I've got all those things installed on my laptop.
If you can somehow make that work, though, running this script will print out a nice little summary of what's failing against which versions of Python and which database. Run with `-v` to see the actual failures.
The loremipsum tag generates random content for use in mockups. It takes this content from Cicero's De finibus bonorum et malorum. By default you get a random number of paragraphs up to 6, but you can affect this behaviour by passing in parameters for quantity and units. Units can be "paragraphs" or "words"; the default is paragraphs. Quantity dictates the number of units returned.
If your django.conf.settings.TEMPLATE_DEBUG is True, then no output is returned, so you should be OK in the event you leave the tag in a production template.
Syntax:
{% loremipsum %}
{% loremipsum quantity="3" units="paragraphs" %}
By leaving the behaviour as random, you can see how your layout handles varying amounts of content. Handy, especially for multi-column pages. [Here's a stoopid site](http://lakes.knoxzilla.com/) where I'm playing with random text, pictures, business listings, etc.
I do plan to add a "corpus" parameter to allow you to put your own corpus between lorumipsum and endloremipsum tags and a template parameter to specify the corpus should come from a template, but this has suited me well thus far.
This is part of the user-registration code used on this site (see [the django-registration project on Google Code](http://code.google.com/p/django-registration/) for full source code), and shows a couple of interesting tricks you can do with manager methods.
In this case there's a separate `RegistrationProfile` model used to store an activation key and expiration time for a new user's account, and the manager provides a couple of useful methods for working with them: `create_inactive_user` creates a new user and a new `RegistrationProfile` and emails an activation link, `activate_user` knows how to activate a user's account, and `delete_expired_users` knows how to clean out old accounts that were never activated.
Putting this code into custom manager methods helps a lot with re-use, because it means that this code doesn't have to be copied over into different views for each site which uses registration, and also makes more sense in terms of design, because these are methods which need to "know about" the model and work with it, and so they belong in a place close to the model.
On WebFaction, each host has it's own Apache instance, with WebFaction's main Apache instance forwarding requests. This is very useful but means that some of the original information is lost. This middleware should be installed at the top of your list to restore this lost info.
It includes the functionality that used to be in SetRemoteAddrFromForwardedFor before it was removed from Django.
This is a field that allows multiple markup types but also stores the pre-rendered result in the database which offers an advantage over calling one of the render methods each time. Example usage looks like:
class BlogPost(models.Model):
...
post = MarkupField()
the various extra fields can then be accessed as follows:
BlogPost.objects.get(pk=1).post # raw content
BlogPost.objects.get(pk=1).post_markup_type # markup type (plain text, html, markdown, rest, textile)
BlogPost.objects.get(pk=1).post_rendered # content of post rendered to html
BlogPost.objects.get(pk=1).post_as_html # property that access post_rendered but marked safe for easy use in templates
After writing my initial version of this I was pointed at the similar http://www.djangosnippets.org/snippets/1169/
I find mine a bit more useful as it includes ReST and includes a mark_safe call to allow showing the rendered HTML directly. I have however borrowed the nice idea of dynamically building MARKUP_TYPES from #1169.
Also available via http://gist.github.com/67724.
A drop-in module to allow for full-text searchable models with very little effort. Tested with PostgreSQL 8.3, but should work on earlier versions with the tsearch2 module installed.
This middleware allows you to easily include the excellent debugging tool Firebug Lite in your projects. To install it, just add the middleware class to your list of installed middleware, pretty much anywhere in the list. If DEBUG is True, and your IP address is in the list of INTERNAL_IPS, Firebug Lite will load. It will, however, only load in browsers that are **not** Firefox, as I'm assuming that you have the **real** Firebug installed in Firefox. If you don't, go install it--what's wrong with you?
Check out http://getfirebug.com/lite.html for more information.
This function emulates the file upload behaviour of django's admin, but can be used in any view. It takes a list of POST keys that represent uploaded files, and saves the files into a date-formatted directory in the same manner as a `FileField`'s `upload_to` argument.
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.