Login

All snippets written in Python

2956 snippets

Snippet List

CurrentSessionIDMiddleware

The middleware assigns a unique identifier for session. The session id doesn't depend of session or whatever else. It only need cookies to be turned on. The session id is reassigned after client close a browser. Identifier of the session could be read from request: request.current_session_id. You can setup name of the cookie in yours settings module (FLASH_SESSION_COOKIE_NAME). request.current_session_id is lazy. It means the ID will be assigned and cookie will be returned to client after first usage.

  • middleware
  • session
Read More

Template Tag for Random Selection of Any Line

These are template tags meant to support the construction of text in a random or seeded random (reproducible) way. Two tags are provided: `seed_randomization` and `any`. Only seed the randomization if you wish to have the options generated the same way each time. Only necessary once per request, if done early enough in the rendering process. Example without seeding: <p> {% any %} One day Once upon a time In a galaxy far, far away {% endany %} a young foolish {% any %}programmer|lawyer|Jedi{% endany %} {% any %} set out began his quest ran screaming {% endany %} to pay his stupid tax. </p> # Possible outcomes: <p>In a galaxy far, far away a young foolish lawyer set out to pay his stupid tax.</p> <p>One day a young foolish programmer ran screaming to pay his stupid tax.</p> Be sure to read the documentation in the code.

  • tag
  • django
  • random
Read More

Middleware to resolve current URL to module and view

Add it to MIDDLEWARE_CLASSES and you will get request.current_view getting values like "connection.views.index" or "django.views.static.serve" - one example usage is to show different help text based on current view name. EDIT: actually, you can use process_view middleware function where you automatically get current_view as first parameter.

  • middleware
  • view
  • url
  • resolve
Read More

TestCase base class to easily temporarily change module values

1. Base your test case off `ModuleTestCase` and set a class attribute containing a dictionary of modules which you want to be able to revert the values of. 2. Use `self.modulename.attribute = something` in your `setUp` method or test cases to change the module's attribute values. 3. The values will be automatically restored when each test case finishes. For the common case of reverting the settings module, just use the `SettingsTestCase` as your base class.

  • tests
Read More

Template filter to turn Twitter names into links

Quick and simple twitterize filter to turn Twitter usernames into profile links on your own sites. Add the filter code to your own template tags (http://docs.djangoproject.com/en/dev/howto/custom-template-tags/).

  • twitter
  • twtterize
Read More

Aggregation class "Sum" with Case

Just use it like below: from downloaded_file import SumCase MyClass.objects.aggregate( sum1=SumCase('salary', case='salary < 4', when=True), sum1=SumCase('salary', case='type', when='director'), )

  • annotate
  • aggregation
  • sum
Read More

Continuing and breaking from loops in Django templates

This snippet makes Django templates support `break` and `continue` in loops. It is actually more powerful than the respective Python statements as it allows breaking and continuing from an outer loop, not just the innermost. `break` and `continue` are implemented as template filters, with the input value being the loop variable. For example, to break from the current `for` loop use `forloop|break`, and to continue from the next outer loop use `forloop.parentloop|continue`. The implementation monkeypatches Django (specifically Nodelist and ForNode) and has been tested on v1.2 with Python 2.6.

  • template
  • loop
  • break
  • continue
Read More

Use MEDIA_URL in flatpages with SSL

This is a copy of [snippet 654](http://djangosnippets.org/snippets/654/), modified to allow dynamic MEDIA_URL, as you might need that for SSL in combination with [snippet 1754](http://djangosnippets.org/snippets/1754/). This is a template filter to enable the use of the MEDIA_URL setting in content from the flatpages database table. It searches for {{ MEDIA_URL }} and replaces it with the current MEDIA_URL added by a context processor. Note: To set up, drop the above code into a file called media_url.py in your templatetags directory in one of your INSTALLED_APPS, and add the filter to your flatpages template like so: {% load media_url %} {{ flatpage.content|media_url:MEDIA_URL }}

  • filter
  • ssl
  • context
  • flatpages
  • processor
Read More

Managing Google AppEngine datastore maintenance

This code defines a decorator that inform users of your site when the Google AppEngine data store is in read-only mode during maintenance periods. Use it as a decorator on your views that require write access to the data store. @requires_datastore_write def update(request): ... Create a `maintenance.html` Django template (or change the name in the code) with the message that the user will see, something like: This application is currently in maintenance mode and some operations are temporarily unavailable. Thanks for trying back later. Sorry for the inconvenience.

  • decorator
  • readonly
  • appengine
  • maintenance
  • capabilities
  • datastore
Read More

NoneWidget

Sometime may be useful to disable the HTML output from formfield to template.

  • widgets
  • widget
Read More