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.
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()
)
...
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 :-)
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 %}
`
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)`
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\-]+)$'
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.
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 %}
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)
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'
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.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.