This snippet provides a form_for_model and form_for_instance function which create newforms that respect the unique and unique_together constraints defined in the model.
One thing about the coding style in this code snippet: Classes are capitalized even if they're passed as arguments. Thus "Model" is a model class while "model" is a model instance.
From [here](http://gacha.id.lv/blog/26/01/2007/django-autocompletefield/) with litle improvements.
[More about Script.aculo.us and Django](http://wiki.script.aculo.us/scriptaculous/show/IntegrationWithDjango)
You may want to access to "global" errors in your template, typically when you have a custom `clean()` method which checks several fields. `{{ form.errors }}` gives an < ul >, which is often not what you want, and `{{ form.errors|join:", " }}` iterates in the keys of `errors` (which are the names of fields, uninteresting).
The global `errors`'s key is "__all__", and Django doesn't allow us to do `{{ form.errors.__all__ }}`.
This method returns an array, like a classic `{{ form.field.errors }}`, and can be used with a join : `{{ form.get_global_errors|join:", " }}`.
When adding fields in the __init__ of a newform, and you don't want the fields to be added *after* the class-attribute fields, this is a possibility... This is a bad example as the email_from could just as well have been defined as a class variable!
This isn't really any trick, its just that I didn't find any documentation of this or any references in searching. There are a few changes proposed for css classes which might make this redundant, but for now this works!
If you want to add attributes for any fields, just include them in the widget constructor. They get written out in key value pairs when the input field is being created. e.g. in the example above, this will come out like:
`
'<input type="text" name="start_date" id="id_start_date" class="vDateField required" size="10"/>'
`
newforms widget for autocompleting text fields using jquery autocomplete plugin: http://jquery.bassistance.de/autocomplete/
to be implemented:
- store the pk value into an hidden field
- handling ChoiceFields and many others
massimo dot scamarcia at gmail.com
Wizard class - subclass this, supply a done() method and then you can use `MyWizard( [list, of, newforms, classes])` as a view function.
see [#3218](http://code.djangoproject.com/ticket/3218)
This is a helper field & widget that lets you ask for a 'user or group'. It presents a select box for whether you wish to enter a user or a group, and then a text field for the name of the user or group you wish to have.
It will validate that the username or group name selected exists.
You would use this like any other field:
`
class AddForumCollectionCreatePermForm(forms.Form):
user_or_group = UserOrGroupField(label = "User or Group",
help_text = "The user or group to grant "
"forum collection create priveleges to.")
`
WTForm is an extension to the django newforms library allowing
the developer, in a very flexible way, to layout the form
fields using fieldsets and columns
WTForm was built with the well-documented [YUI Grid CSS](http://developer.yahoo.com/yui/grids/) in
mind when rendering the columns and fields. This should make
it easy to implement WTForm in your own applications.
Here is an image of an [example form rendered with WTForm](http://www.gmta.info/files/wtform.png).
**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)
Renders an select field with some optgroups. Some options can be outside the optgroup(s).
The options and labels should be in a tuple with ((label, choices),) where choices is a tuple ((key, value), (key2, value2)). If a label is null or blank, the options will not belong to an opt group.
Useful for when you want to use an instance's values as the initial values of a form which you didn't use `form_for_instance` to create.
Handles foreign keys and many-to-many fields just fine.
The newforms package allows you to simply add new field types to your forms.
This snippet shows the addition of a new type of field, to make sure the user enters sensible currency values (either with no decimal, or two-decimal places), and a custom widget to make sure that the value is displayed correctly when the user sees the form.
The CurrencyInput widget simply tries to display the current value in floating point 2-decimal places format (and gives up if it can't). The CurrencyField makes sure that the input value matches a regular expression, and when it is asked to clean the value it converts it to a float.
The regex here limits the amount to 99,999.99 or less. This is within the bounds of accuracy of python's float value. If you want to use truly huge values for the amount, then you'll face problems with the floating point not being able to represent the values you enter, and so the conversion to floating point in the field will fail. In this case it would be better to use python longs, and used a fixed point interpretation.
Using the `run_oldforms_validators` function, you can run oldforms validators in your newforms `clean_XXX` methods.
Usage example:
class PasswordForm(forms.Form):
password = forms.CharField(widget=forms.PasswordInput())
def clean_password(self):
validator_list = [
isStrongPassword,
isValidLength,
SomeValidators(
num_required=3,
validator_list=[hasLower, hasUpper, hasNumeric, hasSpecial],
error_message="Password must contain at least 3 of: lowercase, uppercase, numeric, and/or special characters."
)
]
run_oldforms_validators('password', self, validator_list)
return self.clean_data['password']
Above, `isStrongPassword`, `isValidLength`, etc. are oldforms validators. If you are interested in seeing the implementation of `isStrongPassword`, please see my [Using CrackLib to require stronger passwords](http://gdub.wordpress.com/2006/08/26/using-cracklib-to-require-stronger-passwords/) blog post.