I've gotten this code originally from Dan Fairs. I've edited it a bit to make it do what I wanted it to do. I've sent a mail and the original can be considered public domain code. The same is true for this
A command for manage.py which scans through installed applications the way admin.autodiscover does, just looking for media folders. It then creates a symbolic link to the media under MEDIA_ROOT/app_name.
Usage: save in an apps management/commands folder and run with "python manage.py linkmedia"
Only works on *nix systems, but there should be an equivilent way to do this with windows using .lnk files.
This is a simple rc script, suitable for starting a FastCGI daemon on FreeBSD. Simply copy this into /usr/local/etc/rc.d , change all references to "signet" to the name of your app, mark it executable and modify /etc/rc.conf accordingly.
Sometimes you need to test some model features without a complete django app installation. Just play only with the model object. With these small script you have a complete in memory django installation.
Some Links:
http://www.djangosnippets.org/snippets/1044/ (en)
http://www.jensdiemer.de/permalink/150/mein-blog/99/django-db-model-test/ (de)
http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de)
See also:
https://github.com/readevalprint/mini-django/
This is a simple fixture that is useful for many tests.
It contains the following users:
* admin
* staff
* user0
* user1
* user2
* user3
* inactive0
* inactive1
The password of every user is the same as his username, e.g.: admin/admin
A simple way to add `date_created` and `date_modified` timestamps to a model. Adds a `date_created` timestamp when the object is first created and adds a `date_modified` timestamp whenever the item is saved.
**Note:** You might be tempted instead to use: `date_created=models.DateTimeField(default=datetime.now())` but that won't work as Python will calculate `datetime.now()` only once when it interprets your model. This means that every object created will get the same `date_created` timestamp until you restart your server.
a simple guestbook. the guestbook-area and entries can (and should) be styled by CSS. The template extends a "base.html", which should contain a "content" block.
The Model is very simple without moderation, admin-comment or any other advanced features, but its easy to extend.
i.e. add a Field "active=models.BooleanField()" and add "exclude=['active']" to the forms.EntryForm.Meta class for moderated Entries.
Now you can switch the entries on/off in the admin-interface by setting active=True/False
this snippet is public domain, use for everything you want.
UPDATE: added basic SPAM protection (a do_not_fill field), but you might want to try a captcha-form/Field like snippet 812
This snippet provides a template tag that automatically replaces references to any resource you want
cached forever with a version of the file that is based on the MD5 sum. For an image, you would use
something like:
{% load utils %}
<img src="{% cacheable "/media/images/logo.png" %}"/>
To install it, put a setting in your settings.py file called "DOCUMENT_ROOT", put the python code
into a templatetag-friendly file (e.g. app/templatetags/utils.py), load that template tag, then use
either a string literal, as above, or a variable name to refer to your resource:
<img src="{% cacheable my_media_file %}"/>
The cacheable resource will be used when `DEBUG = False`, but in DEBUG mode, the path you give it will
be passed back untouched (so you don't have a proliferation of cacheable files as you develop).
Django will need write access to the directory you've specified as "DOCUMENT_ROOT" (so it can copy the original
file into a forever-cacheable version).
You'll also need to set up your webserver to serve files called "MYMD5SUMNAME.cache.(js|css|png|gif|jpg)
with an expires header that is far into the future. The goal here is to create a version of your file
that will never have to be downloaded again. If you ever change the original file, the MD5 sum will
change and the changed file's cacheable name will reflect that.
Besides simply changing the name of resources, if the file is a JavaScript or CSS file, and you've
specified `MINIFY = True`, the file will be minified using YUI compressor.
Redirects to the default site (from Django's Sites contrib app), specified by the `SITE_ID` setting.
That's for example useful if you configured your webserver to handle multiple domains with the same virtual host and want to make sure every requests is then redirected to the right domain.
Hi,
I have developed a middleware that enables to view debugging information in production for a single user filtered by useragent or ip. The debug info is appended to the html code as a remark and can be viewed with a view source operation from the browser.
Take a look at http://code.google.com/p/debugview/
Enjoy
This view serves static media and directory indexes for a django application. It should only be used in development, media should be provided directly by a web server in production.
This view assumes a django application stores its media in app/media (which is very common) and the file is referred to in the templates by the last part of a django app path. e.g. As in django.contrib.admin -> 'admin'.
First we check if the media is a request in an application directory; if so we attempt to serve it from there. Then we attempt to provide the document from the document_root parameter (if provided).
To use this view you should add something like the following to urls.py:
`
if settings.DEBUG:
urlpatterns += (r'^media/(?P<path>.*)$', 'site.media.serve_apps', {'document_root' : settings.MEDIA_ROOT})
`
You can then have the admin media files served by setting ADMIN_MEDIA_PREFIX = '/media/admin/'
A simple addition for the urls.py that exposes the 404/500 templates during development. This way you can test how those look. They're mounted under /404/ and /505/ respectively.
Add this at the bottom of your main urls.py.
This filter converts HTML to nicely-formatted text using the text-browser W3M. I use this for constructing e-mail bodies, since it means I don't have to have two templates, one HTML and one plain-text, for each detailed e-mail I want to send. Besides the obvious maintenance benefits, this is nice because Django's templating system isn't well-suited to plain-text where whitespace and line-breaks are significant.
I chose W3M because it renders tables nicely and can take in HTML from STDIN (which Lynx can't do). An alternative is ELinks; to use it, change "cmd" to the following: `elinks -force-html -stdin -dump -no-home`
This is an (overly simple) example of how to manually delete something from the cache.
Usage is simply `delete_url_cache('/some_view/')`
This most likely doesn't work when using Vary headers but does seem to work fine for the most general case of simply needing to programmatically flush the cache after certain actions.
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.