Login

Top-rated snippets

Snippet List

Template tag "ifregex" and "ifnotregex"

Outputs the contents of the block if the second argument matches (or not, depending on the tag) the regular expression represented by the first argument. Usage: {% ifregex "^/comments/" request.path %} ... {% endifregex %} {% ifnotregex "^/comments/" request.path %} ... {% else %} ... {% endifnotregex %}

  • template
  • templatetag
  • regex
  • ifregex
Read More

"an" filter

A template filter which returns "a" or "an" depending on the phonetic value of given text.

  • template-filter
  • grammar
Read More

render_as_template template tag

This is a template tag that works like `{% include %}`, but instead of loading a template from a file, it uses some text from the current context, and renders that as though it were itself a template. This means, amongst other things, that you can use template tags and filters in database fields. For example, instead of: `{{ flatpage.content }}` you could use: `{% render_as_template flatpage.content %}` Then you can use template tags (such as `{% url showprofile user.id %}`) in flat pages, stored in the database. The template is rendered with the current context. Warning - only allow trusted users to edit content that gets rendered with this tag.

  • template
  • tag
  • context
Read More

Template range filter

Easy to use range filter. Just in case you have to use a "clean" for loop in the template. Inspired by [Template range tag](http://www.djangosnippets.org/snippets/779/) Copy the file to your templatetags and load them. [Django doc | Custom template tags and filters](http://docs.djangoproject.com/en/dev/howto/custom-template-tags/)

  • template
  • filter
  • range
Read More

Signal to post new saved objects to Twitter

Post new saved objects to Twitter. **Example:** from django.db import models class MyModel(models.Model): text = models.CharField(max_length=255) link = models.CharField(max_length=255) def __unicode__(self): return u'%s' % self.text def get_absolute_url(self): return self.link # the following method is optional def get_twitter_message(self): return u'my-custom-twitter-message: %s - %s' % (self.text, self.link) models.signals.post_save.connect(post_to_twitter, sender=MyModel)

  • twitter
  • signal
Read More

Ping All Search Engines

Nothing much to see here. Needed this little puppy for work and figured I and others will need it for other projects. Enjoy!

  • search
  • google
  • all
  • ping
  • engines
  • yahoo
  • live
  • ask
Read More

Clone model mixin

Add this as a superclass of any Django model to allow making copies of instances of that model: class Entry(models.Model, CloneableMixin): [...] e = Entry.objects.get(...) e_clone = e.clone() e_clone.title = 'Cloned Entry' e.save() The new object is saved during the clone process and ManyToMany relations are copied as well.

  • model
  • mixin
  • copy
  • clone
  • clonable
Read More

Multiple Choice model field

Usually you want to store multiple choices as a manytomany link to another table. Sometimes however it is useful to store them in the model itself. This field implements a model field and an accompanying formfield to store multiple choices as a comma-separated list of values, using the normal CHOICES attribute. You'll need to set maxlength long enough to cope with the maximum number of choices, plus a comma for each. The normal get_FOO_display() method returns a comma-delimited string of the expanded values of the selected choices. The formfield takes an optional max_choices parameter to validate a maximum number of choices.

  • multiple
  • model
  • form
Read More

Django Paypal

This is a snippet to use Paypal with python. This modifies Mike Atlas's geocept Paypal library. (Which was not working, for me at least) and adds recurring payments. I would try to write an article explaining how to use this and update the snippet.

  • paypal
  • payments
  • payment-processors
Read More

Enforce site wide login

This is based on a snippet contributed by zbyte64 ( RequireLoginMiddleware) but turned on its head. RequireLoginMiddleware enforces authentication for a subset of urls defined in settings.py. This middleware requires authentication for all urls except those defined in settings.py. The aim is to globally enforce site wide authentication without having to decorate individual views. To use, add the class to MIDDLEWARE_CLASSES and then define the urls you don't need authentication for. These go in a tuple called PUBLIC_URLS in settings.py. For example:- PUBLIC_URLS = ( r'project/application/login/', r'project/application/signup/', ) By default, authentication is not required for any urls served statically by Django. If you want to subject these to the same validation, add an entry to settings.py called SERVE_STATIC_TO_PUBLIC with a value of True.

  • middleware
  • authentication
  • login
Read More

improved generic foreign key manager

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.

  • foreignkey
  • generic
  • manager
  • query
  • tuning
Read More

server with debugging backdoor

this starts up two servers - HTTP serving the django application on port 8000 and a port 8001 server that will start an interactive interpreter with any incoming connections. this enables you to have an interpreter in the same process as your server. $ wget http://localhost:8000/someurl/ (...) $ nc localhost 8001 Python 2.5.2 (r252:60911, Jul 8 2008, 21:21:10) [GCC 4.3.1 20080626 (prerelease)] on linux2 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from django.db import connection >>> connection.queries [ ... ]

  • debugging
Read More

Form splitting/Fieldset templatetag

Syntax: `{% get_fieldset list,of,fields as new_form_object from original_form %}` Example: {% load fieldsets %} ... <fieldset id="contact_details"> <legend>Contact details</legend> <ul> {% get_fieldset first_name,last_name,email,cell_phone as personal_fields from form %} {{ personal_fields.as_ul }} </ul> </fieldset> <fieldset> <legend>Address details</legend> <ul> {% get_fieldset street_address,post_code,city as address_fields from form %} {{ address_fields.as_ul }} </ul> </fieldset>

  • fields
  • forms
  • fieldset
Read More

3110 snippets posted so far.