Widget for TinyMCE 3.2.6, a WYSIWYG HTML editor for `textarea`.
**Note:**
> This snippet uses the TinyMCE package thats contains special jQuery build of TinyMCE and a jQuery integration plugin. Anyway, is easily to adapt to standard package.
Usage example:
from django.contrib.flatpages.admin import FlatpageForm
class MyFlatPageForm(FlatpageForm):
content = forms.CharField(widget=TinyMCEEditor())
[TinyMCE download page](http://tinymce.moxiecode.com/download.php)
This is a username field that matches (and slightly tightens) the constraints on usernames in Django's `User` model.
Most people use RegexField, which is totally fine -- but it can't provide the fine-grained and user friendly messages that come from this field.
With this, you can group form fields very easily. Since it does not create any html, you can use it not only to create fieldsets (with tables, lists, etc).
Browse through the installed models using the content types framework.
There are two difference in behavior with respect to the default field:
1. if a model provides a translation for its name (e.g.: verbose_name and/or verbose_name_plural), it shows that rather than a raw model name
2. allow to filter the models shown through the use of `choice` parameter
Example:
`mbf = ModelBrowseField(choices=['User', 'Session'])`
A lot of times we need to insert a specific **CSS class** into a Form instance for it to be rendered in the template.
The current method is to specify the CSS class in the Python code through the form field widget. But it would be easier for the designer to be able to specify the CSS class in the template directly.
For example rather than doing this in your Python code:
'name = forms.CharField(widget=forms.TextInput(attrs={'class':'special'}))`
You can do this in your template:
`{{ form.name|cssclass:"special"}}`
This template filter only works with Form Field instance.
Sometimes the only way to reproduce a bug on a production site is to login as the User who encountered it. This form allows you to login as any user on the site.
**Usage**
@staff_member_required
def login_as(request, template="login_as.html"):
data = request.POST or None
form = LoginAsForm(data, request=request)
if form.is_valid()
form.save()
return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
...
This is an extension of the DecimalField form field that uses my [Currency Object](http://www.djangosnippets.org/snippets/1525/) and [Currency Widget](http://www.djangosnippets.org/snippets/1526/).
I placed my Currency object in the Django\\utils directory and the widget in Django\\froms\\widgets_special.py because I integrated a set of currency objects into the Admin app ( [here](http://www.djangosnippets.org/snippets/1529/) ) and it was just easier to have everything within Django.
UPDATE 07-30-2009: Add the parse_string argument to properly test the string format as per the update to the [Currency Object](http://www.djangosnippets.org/snippets/1525/)
UPDATE 09-15-2009: Properly handle None's in the clean method
The rest of the series: [Currency Object](http://www.djangosnippets.org/snippets/1525/), [Currency Widget](http://www.djangosnippets.org/snippets/1526/), [Currency DB Field](http://www.djangosnippets.org/snippets/1528/), [Admin Integration](http://www.djangosnippets.org/snippets/1529/)
As there is no straight way to re-produce the real tabular inline formsets you get in django-admin, here is how this template has to look like if you do it form your own formsets generated from formset factories.
Problem
=======
The FormPreview class provided by contrib.formtools helps automate a common workflow. You display a form, then force a preview, then finally allow a submit. If the form gets tampered with, the original form gets redisplayed.
Unfortunately, this class can only be used when you have an html form that is backed by exactly one Django form. No formsets, no html forms backed by more than one Django form.
Solution
========
I was asked to create exactly this sort of workflow for a highly complex form + formset. The ComplexFormPreview class provides a base class to help with this problem. As with FormPreview, you must override a few functions.
Code
====
The abstract ComplexFormPreview class can live anywhere on your python path. Import it and subclass is exactly like you would contrib.formtools FormPreview.
The self.state dictionary is passed to all response calls as the context for your templates. Add any objects you need in your template to this dictionary. This includes all forms, formsets, and any additional variables you want in your template context.
Override the parse_params if you need to get any args/kwargs from your url. Save these values in self.state if you want them in your template context.
Override the init_forms method to do setup for all of your forms and formsets. Save all your forms in self.state. You should provide a unique prefix for all forms and formsets on the page to avoid id collisions in html.
*VERY IMPORTANT NOTE*: init_forms is called with a kwargs dictionary. You need to pass **kwargs to all of your form definations in init_forms. This is how the POST data is going to be passed to your forms and formsets.
*VERY IMPORTANT NOTE No. 2*: all of the validation is handled inside the class - all forms will be found and validated, and we will only proceed when everything is found to be valid. This means that you can use the class as a view directly, or provide a thin wrapper function around it if you want.
Override the done method to handle what should be done once your user has successfully previewed and submitted the form. Usually, this will involve calling one or more save() calls to your various forms and formsets.
Because you now have multiple forms, the default contrib.formtools templates don't work. You must make custom templates that reference all of your various forms. The stage_field, hash_field, and hash_value fields are used exactly like the formtools examples. Follow the basic layout demonstrated in the example templates, and substitute your custom forms for the default form.
Example views.py
================
The views.py demonstrated here has many hooks into my project, including using some [complex formset classes](http://www.djangosnippets.org/snippets/1290/). It won't work for you without being customized, but it will demonstrate how to override the default ComplexFormPreview.
If you need a simple select list (picklist) containing all site categories (or some other taxonomic group) and don't want to depend on Javascript, here's how to build a simple category navigator in Django, using HttpResponseRedirect.