NOTE: this is for **newforms-admin**
I need edit links for items on my site, outside of the django admin -- however, I'm lazy, and don't want to build my own edit forms when there's a perfectly nice admin already there.
Trick is, I need to be able to click a link to the django admin edit page for an item, and have it return to the calling page after saving.
This chunk of code does the trick (the "real" version extra cruft, naturally) -- the links will bring up the django admin editor, then return to the calling page after saving.
Automatically create a unique slug for a model.
Note that you *don't* need to do `obj.slug = ...` since this method updates the instance's slug field directly. All you usually need is: `unique_slugify(obj, obj.title)`
A frequent usage pattern is to override the `save` method of a model and call `unique_slugify` before the `super(...).save()` call.
As a demo, I was asked to write a `render_to_file()` function to load a template and render it to a file. Turns out it's amazingly easy, and I think it's a neat trick to have in your bag of tools.
This snippet is extracted from my Photo model. I use it to ensure that any uploaded image is constrained to a specified size (resized on save).
In my case, I don't need to maintain a "thumbnail" and "fullsize" version, so I just store the resized version to save space.
This is an extension to ubernostrum's [comment-utils](http://code.google.com/p/django-comment-utils/) that performs comment moderation based on LinkSleeve check result. It requires original comment-utils package.
A simple adaptation of RegistrationFormUniqueEmail from django-registration http://code.google.com/p/django-registration to allow users to register without using a username. This works great with the code from this comment: http://www.djangosnippets.org/snippets/74/#c195 to allow users to completely eliminate the need for a username.
This filter allows you to format numbers like PHP's [number_format](http://php.net/number_format) function.
If `var` equals 1234.567`{{ var|number_format:2 }}` produces 1,234.56.
Because Django's template filters support just 1 argument you'll have to adjust the argument's default values by hand until [#1199](http://code.djangoproject.com/ticket/1199) is fixed.
This was born as a result of the fact that session data is shared across logins on a single browser. If you login as user1 and session data is stored, then login as user2 the same session data will be available to your application. Please see the ticket who's validity is at this point in question. Some feel that this is normal behavior.
http://code.djangoproject.com/ticket/6941
I use this code in conjunction with
http://code.google.com/p/django-registration/
Place this code in registration.__init__ and change registration.urls to have login and logout route to the new alternate versions alt_login, alt_logout.
I have only been using Python and Django for a couple months now so I hope that this implementation is not too terrible. It works for me. Enjoy.
This filter naively parses HTML content, and inserts <wbr/> tags in lines with unbroken strings longer than max_line_length characters. It leaves content inside tags alone, so that things like urls are unaltered. XHTML entities are treated as atomic, and whitespace is determined with a regex.
It assumes well formed HTML.
This snippet is an example of an ajax progress bar (using jquery) that you might use in conjunction with <http://www.djangosnippets.org/snippets/678/>.
1. Generates a uuid and adds X-Progress-ID to the forms action url.
2. Adds the progress bar to the page. (you'll have to add some css styling for this)
3. Makes period ajax requests to update the progress bar.
Ticket [#2070](http://code.djangoproject.com/ticket/2070) allows you to create your own file upload handlers. Here's an example handler that tracks a file's upload so you might display a progress meter after form submission.
The snippet has two parts, the upload handler which tracks progress, and an upload_progress view used to report back to the browser.
The upload handler uses [django's cache framework](http://www.djangoproject.com/documentation/cache/#the-low-level-cache-api) to store the data, which can then be retrieved by the view to send back to the browser.
Note: Your form's http post request must have a query parameter (X-Progress-ID) sent along with it, which should contain a unique key to identify the upload. That key will also be sent with your ajax requests to the upload_progress view to retrieve the progress data.
Setup: place the UploadProgressCachedHandler anywhere you like on the python path, and add to your settings.py:
from django.conf import global_settings
FILE_UPLOAD_HANDLERS = ('path.to.UploadProgressCachedHandler', ) + \
global_settings.FILE_UPLOAD_HANDLERS
Set up the upload_progress view in any of your apps along with a corresponding entry in your urlconf.
Here's some javascript example code to make the ajax requests and display the progress meter: <http://www.djangosnippets.org/snippets/679/>
.