Two template tags (I keep them in an app called "utils") handy for building menus out of flatpages. I only have two levels of hierarchy, and the frontpage is dynamic, so that's what it does. There is some flexibility, however, in that you can change the regex that defines a "root" page.
You can pass it the flatpage object that is normally given to a flatpage template, or you can pass it a string (for any other page).
Deliberatley does not create any markup -- flexibility is key, so it adds the list of root urls or child urls as a name in the current context. You get to pick the name.
Please visit the [GitHub archive](http://wiki.github.com/0sn/nameremoved/flatpages) where i keep this up to date, there's a better explanation and you can see it in use.
Sometimes it is desirable to use values like the primary key when naming `FileField` and `ImageField` files, but such values are only available after saving the model instance. This abstract class implements a two-phase save in order to make this case easy. See the example in the docstring.
Another solution would be to write a `save()` that requires `upload_to` to be a callable that checks for `instance.pk`, then calls it again after saving. However, this would require more work from the developer for simple cases.
A rewrite of the django.contrib.formtools.FormWizard class to be session-based instead of saving state in hidden form variables.
It started out as a simple port of the standard FormWizard class, and much of the original behavior was unchanged because I wanted to limit the scope of the "rewrite". Along the way I think I made it a bit more OO, and a bit more flexible, but there are still warts.
I threw this code up here before I was completely happy with it just to take part in the discussion around [this ticket][http://code.djangoproject.com/ticket/9200 "Django Ticket #9200]. There's certainly a few things that the patch on that ticket does that I'd like to incorporate into my Wizard, whenever time allows.
This is a quick shortcut to redirect the user to a view. The main gain is avoiding having to type 'from django.core.urlresolvers import reverse' every time you want to do a redirect!
This is a simple implementation overwrite of the FileSystemStorage. It removes the addition of an '_' to the filename if the file already exists in the storage system. I needed a model in the admin area to act exactly like a file system (overwriting the file if it already exists).
When you're using Django model inheritance, sometimes you want to be able to get objects of the base class that aren't instances of any of the subclasses. You might expect the obvious way of doing this, `SuperModel.objects.filter(submodel__isnull=True)`, to work, but unfortunately it doesn't. (Neither does `SuperModel.objects.filter(submodel__supermodel_ptr=None)`, or any other convoluted way I could think of doing it.) Here's a nicer approach for doing this.
[The blog entry is here.](http://sciyoshi.com/blog/2008/aug/07/custom-django-manager-excludes-subclasses/)
The venerable CustomImageField, invented by [Scott Barnham](http://scottbarnham.com/blog/2007/07/31/uploading-images-to-a-dynamic-path-with-django/) and rejiggered for newforms-admin by [jamstooks](http://pandemoniumillusion.wordpress.com/2008/08/06/django-imagefield-and-filefield-dynamic-upload-path-in-newforms-admin/#comments).
This here is a stab at a [post-Signals-refactor](http://code.djangoproject.com/wiki/BackwardsIncompatibleChanges#Signalrefactoring) version. Seems to do 'er.
Note: This should be pointless once [fs-refactor](http://code.djangoproject.com/ticket/5361) lands.
Best practice based on [YSlow recommendations](http://developer.yahoo.com/yslow/), add the following to your Apache config for your media directory.
<Directory /home/.../site_media/>
...
FileETag None
ExpiresActive on
ExpiresDefault "access plus 10 years"
AddOutputFilterByType DEFLATE text/css application/x-javascript
</Directory`
Make sure to enable mod_deflate and mod_expires.
this is an enhancement to paulsmith's code snippet 190, to provide an extra "search by distance" parameter rather than "limit number of results returned".
i need this for a social networking site where users can search for people within a certain geographical area.
Add-on for auth app of newforms-admin branch, adding more information about users and their groups at user and group list in admin interface. Also, nice example on customization of contributed django admin apps.
It adds the following to the user list interface: fields for is_superuser and is_staff, last login time; by default, short names of groups user is in (mousehover to see full names)
It adds the following to the to group list interface: list of users in your groups.
To enable, just put it somewhere and import it from your main urls.py:
import useradmin
** This tag, as shown, can cause problems (infinite recursion being one) if you don't use it correctly. **
Our internal CMS has a pages app similar to Flatpages, and a "chunks" app similar to [django-chunks](http://blog.clintecker.com/2008/jul/6/django-chunks/). Because most of our other apps are template-tag driven (FAQs, job postings, news, events, etc) I wanted a way to be able to write django template code right into a page or chunk's body from within the django admin.
This tag will let you write django template code into, for example, a Flatpage's content and render it with the current context. So if you had a template tag called "get_latest_news" and wanted to add latest news to a flatpage, just enter this in the flatpage's content:
{% get_latest_news 5 as news %}
{% for article in news %}
<li>{{ article.title }}</li>
{% endfor %}
This ability has proven extremely useful to us. Please note this is just a "summary" snippet to illustrate the concept. Use in a production environment should include more robust error checking.
This is a simple helper to make custom permission decorators for Django views.
Perhaps you have an edit_comment view which you want to make sure current user is the owner of:
>def edit_comment(request, comment_id):
>>if request.user == Comment(id=comment_id).user:
>>>... do authorized things ...
>>else:
>>>... do unauthorized things ...
>>...
>>...
In this view, you might do a quick check `if request.user == Comment(id=comment_id).user`, however you now need to duplicate this code all over the place whenever you want to check if a comment is owned by the current user.
Instead, you can use the built in login_required decorator, and your own decorator to do the test:
>@permission
>def user_owns_comment(request, comment_id):
>>return request.user == Comment(id=comment_id)
>
>@login_required
>@user_owns_comment
>def edit(request, comment_id):
>> ...
>> ...
>> ...
The "tester" function will post a message using the messages module built into Django, and redirect the user to the root. It allows access and executes the view if the tester function returns anything that evaluates to True.
Your permission tester should either strictly specify the same arguments as the view, or take additional *args, and **kwargs to prevent syntax errors on extra arguments being passed along.
Script to help manage database migrations. Explanation and background can be found in blog post at [paltman.com](http://paltman.com/2008/07/03/managing-database-changes-in-django/).
This template filter is rewritten, courtesy of Eric Moritz.
It is meant to be used when displaying status messages from Twitter. A regular expression is used to replace all @username replies with a link to that user's Twitter page.
In use at [http://ryanberg.net/blog/statuses/](http://ryanberg.net/blog/statuses/)
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.