Login

Most bookmarked snippets

Snippet List

Django filter stack to cleanup WYSIWYG output

Django Template Tag Filter stack to clean up output from [widgEditor](http://www.themaninblue.com/experiment/widgEditor/) or other WYSIWYG text-input box. Removes arbitrary line break code and replaces with Django's cleaner `|linebreaks` filter. Also removes any arbitrary styling, leaving in things like bold, italic, link and image tags.

  • template
  • filter
  • wysiwyg
Read More

BRPhoneNumberWidget

A widget created for BRPhoneNumberField that splits the input into a <input> for the area code and another for the phone number. Usage example: class MyForm(forms.Form): mobile_phone = BRPhoneNumberField( label='Telefone Celular', widget=BRPhoneNumberWidget() ) ...

  • phone
  • br
  • localflavor
  • BRPhoneNumberField
Read More

Optimistic locking in Admin

Look here: https://bitbucket.org/depaolim/optlock The snippet of Marco De Paoli is much better than this one! :-) ========================================================= If two users save same record, the second one will overwrite first one. Use this snippet to achieve an optimistic locking, so the second user will get an exception. Save the snippet in optlock.py and put it anywhere in your pythonpath. **Do not put _version_opt_lock in readonly_fields** or the snippet will fail! (if you need to hide it, use jquery). The snippet need in request.POST the original value of _version_opt_lock and if you make it a readonly field Django doesn't POST its value. When this [bug](https://code.djangoproject.com/ticket/11277) will be fixed it should be possible to use a hiddeninput widget. models.py example: ... import optlock ... class YourModel(optlock.Model): ... admin.py example: ... import optlock ... class YourModelAdmin(optlock.ModelAdmin): ... That's it :-)

  • admin
  • locking
  • optimistic locking
Read More

grouper tag

I needed to display formset into table and I didn´t like solution I have found. So I have written this simple tag you can use it in templates like this: ` {% for row in formset|square_it:6 %} <tr> <td> </td> {% for form in row %} <td> {% for field in form %} {{ field }} {% endfor %} </td> {% endfor %} `

  • template
  • tag
  • templatetag
  • formset
Read More

Django load global fixtures test helper

This lets you load global fixtures from a directory you set as `FIXTURES_ROOT` in your settings.py. For example, setting `FIXTURES_ROOT` to `/path/to/myproject/fixtures/` I tend to like to keep fixtures that should be loaded when a new instance of a site is deployed, but not auto-loaded (so they won't rewrite any data that comes after), in a project-level fixtures directory like this. Sometimes these fixtures can be useful in your test suites, so this is a convenient way to load them. Usage: `load_global_fixtures('sites.json', 'contacts.json')` `load_global_fixtures('sites.json', 'contacts.json', fixtures_root='/some/other/path', verbosity=1)`

  • fixtures
  • tests
  • helper
Read More
Author: vaz
  • 1
  • 1

Random object IDs using an abstract base model

To put obfuscated primary keys in any class, simply inherit from this one. For example: class Offer(ObfuscatedPKModel) You can match for these bigint primary keys in your urls.py like this: '^offer/(?P<offer_pk>[0-9\-]+)$'

  • models
  • model
  • random
  • abstract
  • primary-key
  • obfuscation
  • obfuscated
  • ID
Read More

Dynamically add css-classes to formfields

This example assumes you have a form and want to highlight one of two fields by setting <class="highlight"> in the html dynamically. This is an alternative to <https://docs.djangoproject.com/en/1.3/ref/forms/widgets/#customizing-widget-instances>, but now you're not limited to assigning the class to the fields html-output, instead you can also assign it to a div around the field like done here. After assigning a css-attribute to a field, we access the css via a templatefilter *{{ field|css }}* that looks up *field.form.fields[field.name].css* and not simply *field.css*, since the latter would try to access a non-existing css-attribute on a BoundField-instance EDIT: The templatefilter is unnecessary. There is a much easier way, since the original field itself is an attribute of the BoundField named 'field'. So in the template, we can access the css via {{ field.field.css }}. Thanks to Tom Evans for pointing me at this.

  • css
  • form
  • class
  • dynamic-form
  • formfield
  • dynamic-css
  • css-class
Read More

Prettify HTML5 middleware

Slightly updated version of http://djangosnippets.org/snippets/172/ . Supports new HTML5 tags (even though tidy doesn't).

  • middleware
  • tidy
  • standard
  • html5
Read More

tag to store a settings value as template variable

Get any value from settings.py as a template variable. The variable can then be used in conditional tags. E.g. to show a link to a help page only if it the help page url is defined in settings.py {% load get_setting %} {% get_setting MY_HELP_URL as help_url %} {% if help_url %}<a href="{% help_url|safe %}">Help</a>{% endif %}

  • template
  • tag
  • templatetag
  • settings
Read More

Read only form & model field

These decorators can be used to make some model/form fields readonly. **Sample usage:** # Use this decorator on form with readonly fields.` @modelform_with_readonly_fields` class FooAdminForm(forms.ModelForm):` ... # This decorator shoud be used to protect selected fields ` # from modification after initial save.` @has_readonly_fields` class Foo(models.Model):` read_only_fields = ('name', )` ... **Result** will be the same as shown in this post: [Readonly field](http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-be/1424453#1424453) and [Readonly model field](http://www.djangozen.com/blog/read-only-fields-in-models)

  • forms
  • model
  • field
  • readonly
Read More

Spanish National Identification Number Field (DNI)

This django field can be used to introduce Spanish national Identification Numbers (NIF). It validates the NIF format and checks if the last letter is correct. It also validates NIE numbers for foreign people. You can use the field in your own forms: 'code' dni = DNIField(max_length=15, widget=forms.TextInput(), label=_('DNI')) 'code'

  • validation
  • dni
  • custom fields
  • nif
  • nid
  • nie
Read More

A slightly better YAML serializer

I find Django's default YAML serializer output too repetitive, so I came up with this customized version that avoids outputting the model name for every single object, and makes the primary key into an index for the object's fields. This allows many simple model instances to be serialized as one-liners. See the module docstring for additional explanation and usage notes.

  • yaml
  • serializer
  • deserializer
Read More

3110 snippets posted so far.