Django app WSGI
If you want to have your wsgi as general as possible (for eg. different environments) without any hardcoded paths, this example might help you...
- wsgi
If you want to have your wsgi as general as possible (for eg. different environments) without any hardcoded paths, this example might help you...
This method is replacement for django test runner. It uses nose test runner, which will discover all tests in the application (not even in tests module of applications). For installing put in settings.py: `TEST_RUNNER = 'nose_runner.run_tests'` where *nose_runner* is module containing the code
Heavily based on [Snippet 1033](http://www.djangosnippets.org/snippets/1033/) and [Snippet 766](http://www.djangosnippets.org/snippets/766/). This snippet tracks what view and templates are used to render HTML responses and inserts a small dialog in the top right corner of the output with links to all the files. If your text editor support opening files from a browser protocol you can click the links to open the files right up! For example TextMate supports the `txmt://` protocol. Really saves some time if you find yourself editing a lot of templates.  **Usage** 1. Save this snippet in a file called `middleware.py` on your Python Path. 2. Add `middleware.EditingMiddleware` to your `MIDDLEWARE_CLASSES`. 3. Browse to any HTML page on your site!
Adds a templatetag that works like an if block, but . The one and only argument is a float that reflects the percentage chance. It defaults to .2, %20. {% sometimes %} <img src='spy_behind_sniper.jpg'/> {% else %} <img src='sniper.jpg'/> {% endsometimes %} -- or -- {% sometimes .001 %} You win! {% else %} Sorry, not a winner. Play again! {% endsometimes %} -- or -- {% sometimes .5 %} This shows up half the time. {% endsometimes %}
This is a conditional templatetag decorator that makes it *very* easy to write template tags that can be used as conditions. This can help avoid template boilerplate code (e.g. setting a variable in your template to be used in a condition). All you have to do is define a function with expected parameters that returns True or False. Examples are in the code.
My admin allows editing of some html fields using TinyMCE, so I end up with horrible code that contains lots of nested `<p>`, `<div>`, `<span>` tags, and style properties which destroy my layout and consistence. This tag based on lxml tries to kill as much unneeded tags as possible, and style properties. These properties can be customized by adapting the regex to your needs.
This class generates a crows-foot notation ERD for your models (currently does not include fields) using GraphViz. The cardinality/modality of the relationships isn't perfect, but it works for 99% of cases out there.
A mixin to define permissions on models. This is more of an abstract model to subclass/customise than a plug-in solution. Explanations are [here](http://www.muhuk.com/2009/05/django-permission-system/).
This snippet is for [django-flag](http://code.google.com/p/django-flag/) Pinax app to make it generic moderator for any content model. You don't need to modify neither your model nor your views to moderate your flagged content objects.
Slightly shonky implementation of a custom URL shortening view, as [described on my blog](http://simonwillison.net/2009/Apr/11/revcanonical/). Depends on [baseconv.py](http://www.djangosnippets.org/snippets/1431/)
This TemplateTag simply returns a True when it is Naked CSS Day allowing you to hide your CSS or display a custom message on that date. Allows allows for parameters should the date change (again).
This is an example how to dynamically import modules other than `models.py` from installed apps. It allows you to define the full module path just once in `INSTALLED_APPS` in the settings. Using this technique makes it possible to write extensible and reusable apps as mentioned in [Abstract Models and Dynamicly Assigned Foreign Keys](http://djangotricks.blogspot.com/2009/02/abstract-models-and-dynamicly-assigned.html). Probably, it would be great to have some helpers for doing this in django itself. For example: from django.db import models from django.utils import importlib def import_installed(path): """ Imports a module from an installed app >>> import_installed("myapp.forms") <module 'myproject.apps.myapp.forms'> """ app_name, module = path.split(".", 1) app = models.get_app(app_name) return importlib.import_module(app.__name__[:-6] + module) UPDATE: Reported as ticket [#10703](http://code.djangoproject.com/ticket/10703)
By enabling this backend: AUTHENTICATION_BACKENDS = ( 'path.to.my.backends.CaseInsensitiveModelBackend', ) Your users will now be able to log in with their username, no matter whether the letters are upper- or lower-case.
Since the admin URLs refactoring, you can easily link to the admin change view thanks to named URLs. I just discovered it, hope it helps!
This is a function wrapping the code from [example](http://docs.djangoproject.com/en/dev/topics/pagination/#using-paginator-in-a-view) from django docs. The required parameters are: `request` which is a `Request` object from a view, and `objects` - a list of objects to paginate. You may want to tune number of items per page by specifying `count` and the name of a GET parameter through `param_name` To use it in your view just wrap the paginated object into a function for example: def someview(request): articles = Article.objects.all() ... some other logics ... return render_to_response(template, {'articles': paginate(request, articles)}
3110 snippets posted so far.