Login

Snippets by SmileyChris

Snippet List

newforms field callback helper

**Now redundant any anything >0.96**, as `form_for_*` methods now have a `fields` attribute `formfield_callback`s are a bit difficult to use, here's a helper method to create a callback function to use with the `form_for_instance` and `form_for_model` methods. Example usage: person_callback = new_callback(exclude=['password', 'can_add_staff', 'is_staff']) def form_for_person(person): return form_for_instance(person, formfield_callback=person_callback)

  • newforms
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

SmileyChris has posted 20 snippets.