**This is a newforms field for XFN relationships.**
It normalizes input by removing excess whitespace, converting to lowercase and removing duplicates.
This field also validates the relationship according to the [XFN profile](http://gmpg.org/xfn/11): `me` can only appear by itself and, at most, one value from each of the family, friendship and geographical categories is allowed.
The `XFN_*` constants would probably be imported from somewhere else in practice, but are included here for simplicity.
This is a multi file upload widget. That does not require adding multiple file input fields. It requires jQuery.MultiUpload (http://www.fyneworks.com/jquery/multiple-file-upload/)
This is a simple FCK editor widget that can be used in newforms in place of Textarea. Obviously it requires [FCKeditor](http://www.fckeditor.net/) and you will need to set the proper import path for that.
**This is a newforms field for OpenID 1 & 2.**
It normalizes and validates OpenID identifiers according to the [spec](http://openid.net/specs/openid-authentication-2_0.html#normalization):
* `xri://=joelwatts` to `=joelwatts`
* `joelwatts.com` to `http://joelwatts.com/`
* `www.joelwatts.com` to `http://joelwatts.com/`
An identifier that is well-formed, but not an OpenID (e.g. `example.com`), will cause a validation error.
Requires [Python OpenID Library](http://openidenabled.com/python-openid/) 2.x.x.
I noticed that form_for_* in newforms now carry deprecation warnings. This code is a minimal demonstration of how to use ModelForm as a replacement.
Full information can be found on [Stereoplex](http://www.stereoplex.com/two-voices/django-modelform-and-newforms). Hope this helps you.
This is a little base class that I use to display forms in a readonly mode. Providing a simple javascript on the front end to allow a user to click an **edit** link that will show/hide the actual form.
To use it simply inherit from it. Then in your templates do the following:
`{{ form.as_readonly_table }}`
**Other Notes**
* **form.html**
`{% for field in bound_fields %}
{% include "newforms/field.html" %}
{% endfor %}`
* **field.html**
`{% if field.errors %}
<tr>
<td> </td>
<td>{{ field.errors }}</td>
</tr>
{% endif %}
<tr>
<th style="text-align:right">
<label for="id_{{ field.name }}">{{ field.label }}{% if field.field.required %}<span class="required"><sup>*</sup></span>{% endif %}</label>
</th>
<td>{{ field }}
{% if field.help_text %}
<p class="helptext">({{ field.help_text }})</p>
{% endif %}
</td>
</tr>`
A Django newforms field which takes another newforms field during
initialization and validates every item in a comma-separated list with
this field class. Please use it like this:
from django.newforms import EmailField
emails = CommaSeparatedValuesField(EmailField)
You would be able to enter a string like "[email protected],[email protected]"
because every email address would be validated when clean() is executed.
This of course also applies to any other Field class.
You can define the sepator (default: ",") during initialization with the
`separator` parameter like this:
from django.newforms import EmailField`
emails = SeparatedValuesField(EmailField, separator="###")
In editing a ManyToMany field in a form, it is necessary to clear the entries in the bridge table before adding new entries. So first save the new entries, remove the old entries in the multiple choice field and then add the new entries. It is also necessary to add the commit_on_success decorator to make sure that the whole process is in one transaction.
the trick resides in `field.field.required`. The intuitive way of testing this in the templates is to access `field.required`. But it's not the good one. Enjoy!
[Found via Django users Google Groups](http://groups.google.com/group/django-users/browse_thread/thread/ce83f74fb1156b4b/0df36947de16a071?lnk=gst&q=required+field#0df36947de16a071)
Example model:
class MyModel(models.Model):
file = RemovableFileField(upload_to='files', \
null=True, blank=True)
image = RemovableImageField(upload_to='images', \
null=True, blank=True)
A delete checkbox will be automatically rendered when editing such a model using form_for_instance.
[UPDATED version which works with ModelForms](http://www.djangosnippets.org/snippets/636/)
You have two models, one of which also has an image field. You want to get the data into a form and edit it. This also requires showing the url of the image, if there is one, on the form. This is a complete working example. No big deal, but for newbies, a complete working example could be valuable.
I use this snippet when creating newforms with form_for_model and form_for_instance. Both do not have an argument that lets you filter out fields easily so I wrote this instead.
Typical use:
`form_class = form_for_instance(obj, fields=ignore_fields(model_class, ['field_name1']))`
I was about to start an online community but every time you allow people to post something as a comment you never know what they come up to, especially regarding profanities.
So I come up with this idea, I put together some code from the old style form validators and the new newform style, plus some code to sanitize HTML from snippet number [169](http://www.djangosnippets.org/snippets/169/), and the final result is a CharField that only accept values without swear words, profanities, curses and bad html.
Cheers.
Makes sure the value a user entered into a a text-based field is automatically trimmed during form cleaning / validation.
The 'field' parameter is expected to be a newforms.fields.Field _instance_.Only modifies str and unicode descending values, and passes everything else on untouched.
Example:
form = form_for_model(Person)
make_trimming(form.fields['name'])