Most modern browsers support the new `<input type="date">`, which allows inputting date using a browser-native date picker with built-in validation. This form widget utilizes this feature by setting the input type to "date" and by changing the value formatting as it should be in the ISO format.
See more about `<input type="date">`: <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date>
A minimally invasive Select widget that looks and acts like a disabled select input, but still submits with the form. It works by rendering the form as disabled, and supplements it's output with a hidden input field with the same name and value.
Tested in Django 3.0, probably works in Django 1.11+
FileField that checks that the file is a valid CSV and if specified in `expected_fieldnames` checks that the fields match exactly.
The widget's `accept` parameter is set to accept csv, text and excel files.
**TODO**: Validate the entirety of the CSV file, not just the headers. But this should be enough for most use cases, as checking the whole file could be computationally expensive for huge files.
Example usage:
people = CSVField(expected_fieldnames=['First Name', 'Last Name'])
If you require lots of forms in your project and do not want to be creating an extended template for each one I propose this solution.
Classes in the html correspond to bootstrap, you can work without them if you do not use bootstrap.
Wraps many Form subclases in order to get a form wizard to treat them as one.
Many Forms will use one step.
**Example:**
`class LanguageVerifiedHumanForm(MultipleForms):
base_forms = [
('language_form', LanguageForm), ('verified_human_form', VerifiedHumanForm)
]`
`class NewGuideWizard(SessionWizardView):
form_list = [
('guide_form', GuideForm),
('multi_form', LanguageVerifiedHumanForm),
]`
If you need to use some source to construct your form (maybe some database info or some user input), or you use the same fields with the same functionality in various forms and need to write the same piece of code for each of them, then you are looking in the right place. With this snippet you won't have those issues any more. :P
This snippet present a way to wean the fields from the form.
The code is made using crispy-forms, but the same idea can be applied without using it.
Best luck!
I am used to load forms directly into modals using dajax but I found out I had to load the scripts using an ajax call from the browser.
You can see here an example of a dynamically loaded form and the function used to load the scripts.
MultiForm and MultiModelForm
Based on a PrefixDict class I wrote and thus very lean. Lacks a little documentation, though
class MyMultiForm(ModelMultiForm):
class Meta:
localized_fields = '__all__'
form_classes = OrderedDict((
('form1', Form1),
('form2', Form2),
))
Subfields are named `form-name` `prefix_sep` `subfield-name`. `prefix_sep` defaults to `-`. For access in templates, use `form.varfields`, which uses `var_prefix_sep` (default: `_`), instead.
multiform.varfields()['form1_field1']
Sometimes we may need to generate a *ModelChoiceField* in which choices are generated at runtime, depending on the locale language. The snippet generates a *ChoiceField* based on a queryset and a specific attribute of the Model, ordering the choices by the attribute content in the locale language.
**Usage example** (inside a form declaration)
country = LazyModelChoiceField(sort_by='name', queryset = \
Country.objects.all, empty_label=_('All countries'), label=_('Country'))
Based on lsbardel LazyChoiceField implementation (snippet 1767)
Custom form widget for rendering an autocomplete (using jQuery UI's autocomplete widget) text input in a template.
This arose from the need to have all fields asking for a state to use autocomplete, so I decided to make this.
A widget for a checkbox multi select, adapted from the RadioSelect widget, which allows you to iterate over each choice in the template rather than outputting the whole thing as a ul in one go.
{{ form.field.label_tag }}
{% for option in form.field %}
<span>{{ option }}</span>
{% endfor %}