Login

Tag "forms"

Snippet List

Django using admin horizontal filter in forms

This snippet uses the admin FilterSelectMultiple widget in normal forms. Earlier I tried this without the **internationalization javascript** and failed, so I looked around the web and found couple of posts, they worked but suggest many customizations. I learnt from them that they had this *jsi18n* javascript included. I included it with mine and it worked. I have written a [detailed post](http://www.rohanjain.in/coding/2011/06/20/django-using-admin-horizontal-filter-in-forms/) about this.

  • admin
  • forms
  • widget
Read More

remove the annoying "Hold down control..." messages

This function mangles a generated form class to remove the Hold down "Control", or "Command"... messages from the help text. This is really a dirty hack awaiting a proper solution to [Django ticket 9321](http://code.djangoproject.com/ticket/9321). This function can be useful for forms in the admin interface that use custom widgets. Basic usage: class MyModelForm(forms.ModelForm): class Meta: model = MyModel class MyAdmin(admin.ModelAdmin): form = remove_holddown(MyModelForm, ('field1', 'field2'))

  • admin
  • forms
  • hack
  • widget
Read More

Get default form data

Given an unbound form, determine what data would be generated from POSTing the form unchanged. The goal is to end up with a dict such that, passed into another form constructor as its data kwarg, form.changed_data == [].

  • forms
  • widgets
Read More

RequestFetchingMixin

More a proof of concept than anything, this mixin addresses a perceived "shortcoming" of django forms: the inability to interact with the current request, and specifically the "current user". Usage: class SomeForm(forms.Form, RequestFetchingMixin): # fields... def __init__(self, *args, **kwargs): if self.request: # interact with self.request? super(SomeForm, self).__init__(*args, **kwargs) if self.request: # more interaction def clean(self): if self.request: # blah blah self.request.user blah Notes: * This reaches "into the past" to pull an existing HttpRequest object, regardless of what name it was bound to, into "the present". * If multiple requests are found (what on earth are you doing??), the one bound to "request" (or the first one found, if that's not available) will be used. * By default it goes up to 5 frames back before giving up, but that can obviously be changed if you think you need it. * This won't work everywhere. self.request is guaranteed to be present, but may be None. * Just because you have a self.request doesn't mean self.request.user will be a valid user. This is subject to all the rules that a regular view's request is subject to (because it *is* a regular view's request!) * This isn't magic; it's just python.

  • forms
  • request
  • mixin
  • request.user
Read More

ReCaptcha for django forms (improved and with remoteip)

My previous snippet with captcha wasn't very portable but Marco Fucci figured out the thing that I couldn't - value_from_datadict function. So all credits go to him and his snippet, I adapted it to my needs, maybe you like my version better - it doesn't need any captcha libraries and let's you modify the widget's html easily. Also I added an option to pass remoteip to google api's verify method. How to use it: In your settings.py add: RECAPTCHA_PUBKEY = 'your recaptcha public key' RECAPTCHA_PRIVKEY = 'your recaptcha private key' After that just import and use ReCaptchaField in your form as you would any other field. That's it. *** Important *** If you want to have peace of mind in case google decided that the remoteip parametr is mandatory then: Derive every form that has the captcha field from ReCaptchaForm and when you create the form object after receiving POST/GET, pass a remoteip parameter like that: form = YourCaptchaForm(data=request.POST, remoteip=request.META['REMOTE_ADDR'])

  • forms
  • captcha
  • recaptcha
Read More

notify admin what fields have changed in form submission

here is some working code from a site that maintains profile information (address, phone number, etc) for users ("Partners"). this snippet handles the submitted form (after validation) and checks to see if any fields have been changed by the partner. if so, it shoots off an email to the admin showing the current profile information with changed fields marked with asterisks.

  • fields
  • forms
  • changed
Read More
Author: pjv
  • 0
  • 2

Semi-Portable recaptcha integration with django forms

It is not so portable and easy as I wanted it to be because of how django forms work - they don't play well with recaptcha. To get it to work: * Add two variables to your app settings, **RECAPTCHA_PUBKEY** and **RECAPTCHA_PRIVKEY** * Derive forms you want to have a captcha from the provided `ReCaptchaForm` class (how to get it working with ModelForm? any ideas?) * * If you override the form's clean method make sure you firstly call the `ReCaptchaForm`'s clean method * * In your view, upon receiving the form data initialize the objects like this `form = YouFormClassDerivedFromReCaptchaForm(remoteip=request.META['REMOTE_ADDR'], data=request.POST)` (or request.GET of course) - this is because reCaptcha needs the user's remote ip.

  • forms
  • captcha
  • recaptcha
Read More

Custom FileField with content type and size validation

Usage described in this blog post: [Django: FileField with ContentType and File Size Validation](http://nemesisdesign.net/blog/coding/django-filefield-content-type-size-validation/) Snippet inspired by: [Validate by file content type and size](http://djangosnippets.org/snippets/1303/)

  • forms
  • validation
  • upload
  • filefield
  • file
  • content-type
  • max_upload_size
Read More

RadioSelectWithHelpText

A Django form widget which displays help text for individual items in a set of radio buttons. It overrides the RadioSelect widget, adding a small bit of HTML after each <input> element, with the help text for each item. It was developed for a Django Dash project I'm working on (called transphorm.me), so isn't as feature-rich as it could be, but if you have any trouble installing it - or if I've miscopied any of my code in my rush - please let me know.

  • newforms
  • forms
  • widgets
  • radioselect
Read More

Combine ProfileForm an UserForm in one.

Using this method you can combine form for standart django.contrib.auth.models.User model and for your project profile model. As now, ProfileForm can be used as usual, and it will also contain UserForm fields.

  • forms
  • user
  • profile
Read More

Widget for CommaSeparatedIntegerField with pre-defined choices

Widget for editing CommaSeparatedIntegerField with a set of checkboxes. Each checkbox corresponds to single integer value. So, choices should be pre-defined for widget. **Example** Assume, we need a registration form for some event with time frame for 3 days, starting at Monday. User can select any of 3 days, so we need to show 3 checkboxes. Here's the basic example for such form: class EventRegistrationForm(forms.Form): days = forms.CharField(widget=NumbersSelectionWidget( ['Mon', 'Tue', 'Wed'], range(1, 4)))

  • forms
  • widget
  • comma-separated
Read More

FieldStack - easy form template rendering

FieldStack simplifies forms template rendering. This is enhanced version of snippet [1786](http://djangosnippets.org/snippets/1786/)

  • template
  • fields
  • forms
  • templates
  • fieldset
  • form
  • object
  • field
  • lazy
  • fieldsets
  • groups
  • stacked
  • descriptor
Read More

141 snippets posted so far.