Login

Tag "forms"

Snippet List

Stacked/Grouped Forms

This snippet provides support to create a bit more advanced forms, eg group fields together vertically and horizontally. It's a bit similar to the options one has about displaying fields in the admin.

  • forms
  • groups
  • stacked
Read More

A Lazy ChoiceField implementation

Sometimes it is useful to have a ChoiceField which calculates its choices at runtime, when a new instance of a form containing it, is generated. And this is what `LazyChoiceField` does. The `choices` argument must be an *iterable* as for `ChoiceField`. **Usage example** from django import forms DynamicApplicationList = [] class MyForm(forms.Form): dynamic_choice = LazyChoiceField(choices = DynamicApplicationList) `DynamicApplicationList` can now be updated dynamically.

  • fields
  • choice
  • forms
  • form
  • field
Read More

Nice form errors

Nicely output all form errors in one block, using field labels rather than the field attribute names.

  • forms
  • template-filter
  • errors
Read More

Field value as plain text which can't be edited by user

Shows field value as plain text which can't be edited by user. Field value (or key value for foreign keys) is stored in hiddden input. Value of field is stored in hidden input and current value is placed as plain text. User can't change it's value. If field is foreign key then additional attribute 'key' should be set to True (key is stored in hidden field and unicode value is visible): self.fields['my_field'].widget = HiddenInputWithText(attrs={ 'key' : True }) There is one condition: for foreign key field its name have to be same as lowercased model name. Default 'key' value - False is correct for non-foreign key fields: self.fields['my_field'].widget = HiddenInputWithText()

  • forms
  • hidden
  • widget
  • input
Read More

TinyMCE Widget

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)

  • forms
  • wysiwyg
  • form
  • widget
  • modelform
  • tinymce
Read More

CharField powered Tags with ChoiceField widget.

Shell example: >>> pprint.pprint(settings.FORUM_TAGS) ((u'x11', 'Xorg'), (u'pacman', 'Pacman'), (u'aur', 'AUR'), (u'abs', 'ABS'), (u'howto', 'HOWTO'), (u'instalacja', 'Instalacja'), (u'offtopic', 'Offtopic')) >>> t = Thread.objects.all()[0] >>> pprint.pprint(t.tags) [{'slug': u'pacman', 'title': 'Pacman'}, {'slug': u'abs', 'title': 'ABS'}, {'slug': u'howto', 'title': 'HOWTO'}, {'slug': u'instalacja', 'title': 'Instalacja'}, {'slug': u'offtopic', 'title': 'Offtopic'}] >>> t.tags = [{'slug': 'abs'}, {'slug': 'howto'} >>> t.save() >>> t = Thread.objects.get(pk=t.pk) >>> pprint.pprint(t.tags) [{'slug': u'abs', 'title': 'ABS'}, {'slug': u'howto', 'title': 'HOWTO'}] >>> t.tags = ['Offtopic', 'HOWTO'] >>> t.save() >>> t = Thread.objects.get(pk=t.pk) >>> pprint.pprint(t.tags) [{'slug': u'howto', 'title': 'HOWTO'}, {'slug': u'offtopic', 'title': 'Offtopic'}]

  • tag
  • forms
  • field
  • selectmultiple
  • checbox
Read More

Limit ManyToMany fields in forms

Limit ManyToMany fields in forms. Hide the field, if only one item can be selected. e.g. For limit sites choices only to accessible sites. Also available via django-tools: http://code.google.com/p/django-tools/

  • forms
  • manytomany
Read More

Facebook event selector field

Ripped this out of a project I'm working on. The field renders as two <select> elements representing the two-level hierarchy organized events Facebook uses. Returns the id's Facebook wants.

  • forms
  • field
  • widget
  • facebook
  • fbml
  • fbjs
Read More

EmailListField for Django

A simple Django form field which validates a list of emails. [See this at my blog](http://sciyoshi.com/blog/2009/aug/08/emaillistfield-django/)

  • fields
  • forms
  • email
  • form
  • field
  • email-list
Read More

UsernameField (for clean error messages)

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.

  • fields
  • forms
  • user
  • auth
  • form
  • field
  • username
  • users
  • authorization
Read More

FeaturedModelChoiceField

Here is a way to get a drop down list from a queryset, with a list of "featured" items appearing at the top (from another queryset). This can be used for long select boxes which have a subset of commonly used values. The empty label is used as a separator and values can appear in both the featured set and the full set (it's more usable if they are in both). For example a country drop down list with 5 featured countries might look like this: Andorra Australia Kazakhstan Singapore Turkey ------------ Afghanistan Albania Algeria American Samoa Andorra Angola (hundreds more) To use this, define your form field like this: country = FeaturedModelChoiceField(queryset=Country.objects.all(), featured_queryset=Country.objects.featured())

  • fields
  • forms
Read More

Dynamically change a form select widget to a hidden widget

This is an example of how you change a ChoiceField select widget into a hidden field if the right GET variable is passed. In this example code it would change the select widget into something like the following if something like "?d=3" was passed. `<p><label for="id_designation">Designation</label>Designation Option<input type="hidden" name="designation" value="3" id="id_designation" /></p>`

  • dynamic
  • forms
  • select
  • hidden
  • widget
Read More

CodeLookupField

This is a custom form-field designed to make those optional "enter a discount code" fields a little easier to deal with. You create one like this: code = CodeLookupField(model=MyModel, field_name='slug', max_length=20) And then when you validate your form, cleaned_data['code'] will be the actual object if anything was retrieved, or None if the user entered a bad piece of data.

  • forms
  • field
Read More

LoginAsForm - Login as any User without a password

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) ...

  • forms
  • login
  • auth
  • authenticate
  • form
  • auth.contrib
Read More

141 snippets posted so far.