Multiple-value field for Django forms
Project description
# Multiple-value field for Django forms
## The problem
Say we want to use a Django form to validate form input such as the following:
```
?foo=spam&foo=egg&foo=chips
```
Django can parse this query-string to generate a list:
```
>>> request.GET.getlist("foo")
['spam', 'egg', 'chips']
```
...but it is not yet capable of validating the input using `django.forms`.
## How to use
Say for example, we want to validate a list of email addresses, with a query-string such as the following:
```
?email=foo@bar.com&email=spam@egg.com
```
Here are the steps we would take:
1. Install this module (e.g. using `pip install django_multivalueformfield`)
2. Define a form like the following:
```
import django.forms as forms
from multivaluefield import MultiValueField
class MultiEmailForm(forms.Form):
emails = MultiValueField(forms.EmailField(), "email")
```
3. Pass the `QueryDict` into the form and validate as per usual:
```
form = MultiEmailForm(request.GET)
if form.is_valid:
emails = form.cleaned_data["emails"]
# do something with emails
else:
errors = form.errors
# do something with errors
```
## The problem
Say we want to use a Django form to validate form input such as the following:
```
?foo=spam&foo=egg&foo=chips
```
Django can parse this query-string to generate a list:
```
>>> request.GET.getlist("foo")
['spam', 'egg', 'chips']
```
...but it is not yet capable of validating the input using `django.forms`.
## How to use
Say for example, we want to validate a list of email addresses, with a query-string such as the following:
```
?email=foo@bar.com&email=spam@egg.com
```
Here are the steps we would take:
1. Install this module (e.g. using `pip install django_multivalueformfield`)
2. Define a form like the following:
```
import django.forms as forms
from multivaluefield import MultiValueField
class MultiEmailForm(forms.Form):
emails = MultiValueField(forms.EmailField(), "email")
```
3. Pass the `QueryDict` into the form and validate as per usual:
```
form = MultiEmailForm(request.GET)
if form.is_valid:
emails = form.cleaned_data["emails"]
# do something with emails
else:
errors = form.errors
# do something with errors
```
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django_multivalueformfield-0.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e750ca292ff4a2ddb406ecd8d87aebc33c2490a9aab122cba7777d2ae8cc1792 |
|
MD5 | 92d4df022029d4f407deb91f1e40a11c |
|
BLAKE2b-256 | 307d57e1d6639e4dc723214793ab869cc7c3ea28d0adf5fdc9488082cb946b68 |
Close
Hashes for django_multivalueformfield-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99e8686fb2b87a8a76f686f4a971d62741c321079713a28ea004baa17d9de379 |
|
MD5 | 47d4f046ca1da03efef9723674c7fa34 |
|
BLAKE2b-256 | 46f25433570a2e4bdd03768a81db6a831daf80633efd3f136d81d6209856fed9 |