Dynamic Backends
This allows various implementations of a common interface to be loaded. Back end modules can be specified in settings.py, and from there be loaded and treated polymorphically by an application.
- backend
- dynamic-factory
This allows various implementations of a common interface to be loaded. Back end modules can be specified in settings.py, and from there be loaded and treated polymorphically by an application.
This is a mod I made to the Django simple_tag system to let the simple_tags access comments. I plan to try and get it integrated into the trunk, so it's mainly here so (a) the people on django-developers can see it, and (b) while I'm waiting, or if it doesn't get put in the trunk, people can use it. **Installing** 1. Open the module `django.template.__init__`, wherever that lives. 2. Scroll down to the beginning of " `class Library:` " 3. Find the simple_tag function (" `def simple_tag(self,func):` ") 4. Replace the function (even the whitespace before each line) with the code snippet. **Usage** 1. When defining a simple tag (see the [docs](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#shortcut-for-simple-tags) for more info), have the first parameter in your function be named "`context`". Otherwise, an error will be thrown. It can accept other parameters like normal. 2. Use `register.simple_tag(my_function_name, takes_context=True)` to register your function as one that will use the context. 3. The tag's function can access the context like a dictionary - to get a value, just use `context['cheese']`, or to set one, use `context['cheese'] = 'Limberger'`. Due to the context's mutability, this will affect the context in the main template as well. **Notes** This code hasn't been tested in a "production environment", but I did test my modifications thoroughly, and if you don't add `takes_context=True`, `simple_tag` will behave exactly as normal. Of course, if there is a problem, make sure you leave a comment. **Code** Since most of the code is hacked up from other Django library functions, and to prepare for if and when it's merged into the trunk, it's released under the BSD license.
**updated 12/16/08** I run several blogs by visual-artists and web-designers who want a quick way to insert images into their blog without the awkwardness of WSYIWYG and without the technicality of code (eww...). Thanks in advance for your input. #Syntax in a blog goes: [[thumb:the-image-slug]] # Gives you a thumbnail [[image:the-image-slug]] # Presents full-size-image Then of course: {% blog.post|yeagowiki %} You will also need to create some templates (see snippet). Here's a sample: <!-- /templates/photologue/image_snippet.html --> <div class="photologue-image"> <a href="{% if url %}{{ url }}{% else %}/media/{{ image.image }}{% endif %}"> <img src="{{ image.get_display_url }}" /> </a> <p class="photologue-image-caption">{{ image.caption }}</p> </div>
This attaches a signal to the save and delete signals for the Vote object and recalculates the score for the object and stores it in that object's vote_score attribute. This allows you to have a list of those objects and not have to calculate in the database the vote score for each object.
This is like snippet 786, except it does it at the middleware layer, so you don't have to reference it in every view. Super simple. refer to: http://www.djangosnippets.org/snippets/786/
Example: @limit("global", 3, 10, per_ip=True) def view(request, ...): The example limits the view to one request every 3 seconds per ip address. The limit is shared by every view that uses the string "global" (first parameter), which is an arbitrary string. Request succeed until the accumulated requested time in seconds (second parameter) exceeds the limit (third parameter).
A form with built-in CSRF protection. Include CsrfCookieMiddleware in your MIDDLEWARE_SETTINGS, subclass SafeForm and off you go. See: [this django-developers post](http://groups.google.com/group/django-developers/browse_thread/thread/2c33621003992d07?hl=en) for more info. [edit] This form is actually WAY overengineered currently. Will update soon.
This middleware redirects the request for yoursite.com/feed/whatever/onefeed to your feedburner *onefeed* feed. Having ``FEEDBURNER = ('SomeName', ('blog', 'comments', 'tag1'))`` will use the feedburner feeds at http://feedproxy.google.com/SomeName/blog http://feedproxy.google.com/SomeName/comments http://feedproxy.google.com/SomeName/tag/tag1 you can add more tags, or even intersection and union of them the same way (thanks to piranha for the idea of a middleware) **Update:** now it works for tags as well
This piece of code will clear the cache, whether you are using in-memory or filesystem caching.
This is an improvement on [snippet 984](http://www.djangosnippets.org/snippets/984/). Read it's description and [this blog post](http://zerokspot.com/weblog/2008/08/13/genericforeignkeys-with-less-queries/) for good explanations of the problem this solves. Unlike snippet 984, this version is able to handle multiple generic foreign keys, generic foreign keys with nonstandard ct_field and fk_field names, and avoids unnecessary lookups to the ContentType table. To use, just assign an instance of GFKManager as the objects attribute of a model that has generic foreign keys. Then: MyModelWithGFKs.objects.filter(...).fetch_generic_relations() The generic related items will be bulk-fetched to minimize the number of queries.
This a wizard tool similar to the one in django.contrib.formtools, but it uses a session. I hope to eventually get this into shape and contribute it to the formtools package, but for right now, here it is. The wizard steps are broken into 2 categories: Show Form and Submit Form Each category has it's own hooks for manipulating the process, for instance you can short-circuit a process_submit_form() and go straight to done() via a return from preprocess_submit_form(). Sorry for the lack of documentation on this. Here's an example urls.py entry : `(r'^estimate/(?P<page0>\d+)/$', EstimateWizard([EstimateCustomer, EstimateJob, EstimateRoom]))` where EstimateCustomer, Job, and Room are Form classes and EstimateWizard extends SessionFormWizard
This form subclass helps protect against cross-site request forgery by adding a hidden field named `csrf_token` to forms. The form must be initialized with the request as a keyword argument, both with and without POST data: my_form = MySignedForm(request=request) ... my_form = MySignedForm(request.POST, request=request) Upon validation, a `PermissionDenied` exception will be raised if forgery is detected. If any security details have been overlooked in this recipe, please leave a comment.
Take the legwork out of processing forms. Most people have a very specific structure to how they process forms in views. If the request method is "GET", then some HTML (with the blank form) is rendered. If the method is "POST", then the form is validated. If the form is invalid, some other HTML is displayed. If valid, the data is submitted and processed in some way. In order to do this all in a much nicer way, simply subclass `FormHandler`, define three methods (`valid`, `invalid` and `unbound`), point to the form, and use the subclass as your view in the URLconf.
This shows a simple way of adding your project current SVN to the django debug toolbar by creating a custom debug panel.
I've gotten this code originally from Dan Fairs. I've edited it a bit to make it do what I wanted it to do. I've sent a mail and the original can be considered public domain code. The same is true for this
2955 snippets posted so far.