Keeps database table names out of custom SQL code, but still allows for correct parameter passing in the execute function. (psycopg doesn't substitute table or field names, only data, in the execute function)
*NOTE Stephen updated his original snippet http://www.djangosnippets.org/snippets/85/ to work with WebFaction, please use his version now*
This code is 95% from Stephen Zabel's snippet at http://www.djangosnippets.org/snippets/85/. However, his snippet, as it was, wouldn't work when enabling SSL for the admin site on Webfaction. For some reason, the default request.is_secure() doesn't behave properly with Webfaction's setup and redirects.
One thing Webfaction does do is add X-Forwarded-ssl='on' to any https requests. So instead of using request.is_secure(), I've just used that.
To setup the admin site with SSL on Webfaction, do the following:
1. Install this middleware wherever you like, and add it to settings.py
2. In your Webfaction panel, create your 'django' application ("application" in the Webfaction sense, not the Django sense)
3. Create two sites. The first, your main site (which I'll call example.com), should use the application 'django' mounted at '/'. Do *not* have HTTPS enabled on this site.
4. For the second site, also use the application 'django', and again mount it to '/', but this time enable HTTPS.
5. In your urls.py, modify the admin URL as follows: `(r'^admin/', include('django.contrib.admin.urls'), {'SSL':True} )`
That should be it! The admin section of the site now requires SSL (as specified in urls.py), and if anyone tries to access the admin via regular http, a redirect to https will occur.
This code will add a thumbnail image to your Model's Admin list view. The code will also generate the thumb images, so the first view may be a little slow loading.
This assumes you have an **ImageField** in your Model called **image**, and the field's **upload_to** directory has a subdirectory called **tiny**. You then must add **"thumb"** to your Model's Admin **list_display**.
The thumbnail images are also linked to the full size view of the image.
I found this **VERY** useful... hope someone else does as well.
1) You've ported an existing web site to Django.
2) The new site URLs don't have html (or htm) extensions.
3) The old site had URLs with html extensions.
4) You want existing bookmarks to work.
Use this middleware to removes trailing .htm and .html extensions from incoming URLs (GETs only) so that your new site honors existing bookmarks. Locate it in settings.MIDDLEWARE_CLASSES near CommonMiddleware because it has similar middleware stack location requirements. If an incoming URL has an html extension, ZapDotHtmlMiddleware strips it out and redirects.
When adding fields in the __init__ of a newform, and you don't want the fields to be added *after* the class-attribute fields, this is a possibility... This is a bad example as the email_from could just as well have been defined as a class variable!
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.)
This isn't really any trick, its just that I didn't find any documentation of this or any references in searching. There are a few changes proposed for css classes which might make this redundant, but for now this works!
If you want to add attributes for any fields, just include them in the widget constructor. They get written out in key value pairs when the input field is being created. e.g. in the example above, this will come out like:
`
'<input type="text" name="start_date" id="id_start_date" class="vDateField required" size="10"/>'
`
newforms widget for autocompleting text fields using jquery autocomplete plugin: http://jquery.bassistance.de/autocomplete/
to be implemented:
- store the pk value into an hidden field
- handling ChoiceFields and many others
massimo dot scamarcia at gmail.com
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>)
Allows the selection of one or more items from a list.
The built-int `random` filter only allows you to select a single at a time, and repeated use can return the same item many times.
**Example:**
`{% for random_item in item_list|several_random:3 %} ... {% endfor%}`
**Note:** If you're running this on an uncached QuerySet, it can result in many database queries... a future improvement might check to see if the passed object is a QuerySet and act accordingly.