Login

Most bookmarked snippets

Snippet List

Create multiple related objects at once

The following code takes two related models and creates one user interface to for create and update operations that handles them both. It enables the creation or update of instances from both models in one shot, without having to create very complex forms. As I could not find examples for creation of related objects together, I thought this might be useful for someone.

  • forms
  • object-creation
  • related-objects
Read More

Non-Javascript select list navigator

If you need a simple select list (picklist) containing all site categories (or some other taxonomic group) and don't want to depend on Javascript, here's how to build a simple category navigator in Django, using HttpResponseRedirect.

  • form
  • select
  • picklist
Read More

Custom Admin Redirects

This little bit of code will let you reference parts of the admin but lets you control where it returns to. For instance, I have a list of objects in the admin and i want to have a delete link for each object. I don't want them to return to the changelist after a delete however, i want them to return to my list. cheers

  • admin
  • redirect
Read More

django-noserun for testing

I wanted to use Nose with Django so I came up with this. `TEST_RUNNER = 'noserun.run_tests'` in settings.py It does not do setup/teardown implicitly between test methods, you need to call *nosetest.test.flush()* and *nosetest.test.loaddata()* manually if you want that. Enables the method names *setup* and *teardown* The environment variable *NOSE_COVER* runs coverage tests and *NO_DROPDB* preserves the test db.

  • testing
  • test
  • nose
Read More
Author: mjt
  • 1
  • 2

Significant digits filter

Formats float values with specified number of significant digits (defaults to 3). Usage: `{{value|sigdig}} # with 3 significant digits by default` `{{value|sigdig:digits}}` Examples: `{{0.001432143|sigdig}}` renders as `0.00143` `{{874321.4327184|sigdig}}` renders as `874000` `{{874321.4327184|sigdig:5}}` renders as `874320` Useful for scientific or engineering presentation.

  • filter
  • filters
  • math
  • engineering
Read More

Testing Email Registration

http://steven.bitsetters.com/articles/2009/03/09/testing-email-registration-flows-in-django/ Testing email registration flows is typically a pain. Most of the time I just want to sign up with a test user, get the email link and finish the flow. I also want to be able to automate the whole process without having to write some SMTP code to check some mail box for the email. The best way I’ve found to do this is just to write out your emails to some file instead of actually sending them via SMTP when your testing. Below is some code to do just that. I’ve also created a Django management script that will open the last email sent out from your application, find the first link in it and open it in your web browser. Quite handy for following email registration links without logging into your email and clicking on them manually.

  • email
  • debug
  • testing
Read More

Language-aware template inclusion

Looks up for a template based on the template-name plus the current users language code. Loads the template and renders it with the current context. Example:: {% langinclude "foo/some_include.html" %} Based on the users LANGUAGE_CODE, assumed we have 'de', it tries to render the template 'foo/some_include.html.de'. If that doesn't exists, it renders the template 'foo/some_include.html'. This is the default behavior of the include-Tag. Basically this is a shortcut for the following code, just with a fallback for the default template:: {% ifequal LANGUAGE_CODE "de" %} {% include "foo/some_include.html.de" %} {% else %} {% include "foo/some_include.html" %} {% endifequal %} --- Ein deutscher [Weblogeintrag mit Beschreibung](http://www.mahner.org/weblog/sprachabhangige-template-imports/)

  • templatetag
  • i18n
  • language
  • include
Read More

SWFUpload auth decorator

I use this snippet to simplify my auth system with flash uploader SWFUpload. flash_login_required ensures that the user is authenticated and inject the context dictionnary into the specified template. To redirect a user, just set the variable `context['redirect']` with an url. Remember to include the cookie js in your template to get the sessionid variable POSTed to your view: `<script type="text/javascript" src="/static/js/swfupload/swfupload.cookies.js"></script>`

  • authentication
  • decorator
  • auth
  • swfupload
  • login_required
Read More

getting the related entries with a templatetag using django-tagging

Django tagging by default doesn't provide a templatetag to get the related objects for another object. Even though this is implemented as a model. Still, one can use the existing templatetags to achieve the same outcome. Of course, writing a custom templatetag would be more efficient in terms of database access.

  • templatetag
  • tagging
  • related
  • django-tagging
Read More
Author: V
  • 0
  • 2

Unit Test Profiling

A test runner for Django unittests which profiles the tests run, and saves the result. Very useful for diagnosing your apps. Place the top portion of the code into a file called `profiling.py` someplace in your python path. Update your `settings.py` file with the bottom two lines of the code. Now you are ready, so just run `python manage.py test [appnames...]` to test any apps listed with profiling. By default this will just print a nice report after the unittests. If you change the value of `TEST_PROFILE` to a file, the profile will be saved to that file. The latter is recommended because these profiling reports have a lot of info in them, so it is best to tear through them with the `pstats` module.

  • profile
  • unittest
  • cprofile
Read More

3110 snippets posted so far.