Login

All snippets written in Python

Snippet List

Generate a CSV file for a model

This code generates a CSV file for a model. The URL is http://example.com/spreadsheets/app_label/model_name/ (replace spreadsheets for admin, from the URL for the model's admin page.)

  • csv
Read More

Authenticate with Email Address

This code gives you the ability to authenticate a user by their email address instead of the username. I've also added the ability to authenticate via LDAP. Some code used from this [snippet](http://www.djangosnippets.org/snippets/74/)

  • admin
  • user
  • email
  • authenticate
  • ldap
Read More

Django Registration with GMail account

This code works with Django-registration app found here: http://code.google.com/p/django-registration/ If you plan to use this django-registrtion code in your website to allow user registration but do not run your own email server,this snippet can be handy. It uses gmail server to send out email. You have to install libgmail module first. Add two lines to your `settings.py` GMAIL_USERNAME = '[email protected]' GMAIL_PASSWORD = 'your_password' Change models.py - create_inactive_user(...) as given above

  • registration
  • email
  • gmail
  • django-registration
Read More

django-tagging clouds template tag

template tag for producing tag clouds for a given model, using django-tagging application: http://code.google.com/p/django-tagging/ Sample: http://skam.webfactional.com/tags/ Usage: {% tags_cloud_for_model articles.Article as object_list %} {% tags_cloud_for_model articles.Article as object_list step 6 %}

  • template
  • tag
  • tags
  • tagging
  • templatetags
  • clouds
Read More
Author: skam
  • 13
  • 59

MediaWiki Markup

This is a copy paste job of mediawiki's syntax parser built in Python. You'll probably have to edit it to fit your needs MediaWiki-style markup parse(text) -- returns safe-html from wiki markup code based off of mediawiki

  • markup
  • mediawiki
Read More

Subclassing a model.field for newforms

The newforms fields are more capable than the django models fields. In this snippet, we subclass models.IntegerField, and add min_value and max_value. e.g. age = MMIntegerField('How old are you?', min_value=13, max_value=120)

  • newforms
  • fields
  • model
Read More

TestCase helpers

I use these helper methods in my unit tests. They turn many simple getting-and-posting tests into one-liners. Definitely a work in progress, and I can't be the only person who has done this sort of thing -- comments are more than welcome.

  • testing
  • tests
  • unittest
Read More
Author: pbx
  • 10
  • 11

Digg Style URL String Parser

Does a digg url effect to a string, can be useful for using an item's title in the url, from this: .hi's., is (a) $ [test], will it "work"/ \ to this: his_is_a_test_will_it_work I understand this isn't a very well made script, I am not very good at string manipulation. But I would be happy if someone would recode it in a faster, more managable way. I recomend saving the rendering.

  • url
  • clean
  • simatic
Read More

WordWrap template tag

Basically a clone of the default "wrap" filter. I use it to generate plaintext e-mail that has to be broken at 75 characters. {% wordwrap 80 %} Some text here, including other template tags, includes, etc. {% endwordwrap %} I prefer this over the {% filter wordwrap:80 %} as my template (e-mail) writers keep screwing it up.

  • template
  • tags
  • wordwrap
Read More

Better render_to_response

I'm finding this much more efficient (from a coding perspective) than using the `render_to_response` shortcut. It looks complex, but it's simple to use: just look at the example in the docstring. Call this script `renderer.py` and chuck it in your project root.

  • render_to_response
  • decorator
Read More

{% with %} template tag

Add a value to the context (inside of this block) for easy access. Provides a way to stay DRYer in your templates. **NOTE:** This tag is now in Django core, so if you have Django >0.96 (or SVN) then you do NOT need this.

  • template
  • template-tag
  • encapsulation
Read More

Honeypot Field

Simple anti-spam field which will cause the form to raise a `ValidationError` if the value in this field changes. Displays as a CSS hidden `<input type="text" />` field. If you specify a `class` in the `attrs` of the widget, the default `style="display:none;"` won't be rendered with the widget so that you can use a predefined CSS style to do your hiding instead. You can also cause the widget to be wrapped in an html comment to ensure it is not visible to the end user: class EmailForm(Form): email = EmailField() website = HoneypotField(widget=HoneypotWidget( attrs={'class':'fish'}, html_comment=True))

  • spam
  • anti-spam
  • bot
Read More

PyIf Template Tag (Conditional Tag)

Cheers to limodou for getting me thinking about this. The only problem with his implementation is that it doesn't support Django's "." syntax for accessing array/dict elements. In the Django style of allowing simple syntax for designers while allowing for greater flexibility, and less template duplication for conditionals that were previously impossible to represent in templates, I modified Django's built-in If tag. This is an adaptation/enhancement to Django's built in IfNode {% if ... %} that combines if ifequal ifnotequal into one and then adds even more. This Supports 1. ==, != 2. not .... 3. v in (1,"y",z) 4. <=, <, >=, > 5. nesting (True and (False or (True or False))) How to use it: {% pyif i == 1 or (5 >= i and i != 7) and user.first_name in ('John', 'Jacob') %} 'Tis true. {% else %} 'Tis false. {% endif %} I hope you like it.

  • template
  • tag
  • templatetag
  • if
  • conditional
  • ifequal
  • ifnotequal
Read More

2955 snippets posted so far.