Use this to display an object on a page AND be able to process a form on this page.
This is just a copy of the [Django docs advice](https://docs.djangoproject.com/en/2.2/topics/class-based-views/mixins/#using-formmixin-with-detailview), but put as a reusable, standalone View.
I had a difficult time understanding how to delete an item from a table within a template, using a modelform. I couldn't find a good example, so I wanted to post the code that ultimately worked.
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.
## required
* `{% load trans%}`before using this snippets
* Add this [template filter](https://djangosnippets.org/snippets/2253/) to your custom templatetags and load it before using this snippets
* Bootstrap framework
The simplest way of displaying a "details" table about any model, is to show a ModelFrom with all fields readonly or (selects) disabled.
Also, the labels are preferably translatable, not just capitalized names of the column tables in your models. So the constructor translates the field labels as well.
Function that takes a bound form (submitted form) and returns a list of pairs of field label and user chosen value.
It takes care of:
1. fields that are not filled out
2. if you want to exclude some fields from the final list
3. ChoiceField (select or radio button)
4. MultipleChoiceField (multi-select or checkboxes)
Usage:
if form.is_valid():
form_data = get_form_display_data(form, exclude=['captcha'])
It's trivial to display the list of pairs in a template:
{% for key, value in form_data %}
{{ key }}: {{ value }}{% endfor %}
Validate form field that include email or emails separated by 'token' kwargs, by default ',' a comma. Return a list [] of email(s). Check validity of the email(s) from django EmailField regex (tested with 1.3, but normally will also work with 1.5)
**Use case:**
Suppose you are working on maintenance screens on some data objects. On one particular page (a form) you want to have exits directly back to a calling page (as a cancel operation) or indirectly back to the calling page (after an data update operation). However, the form and its object are not directly related to the target page (perhaps a one-to-many relationship) so you cannot figure the calling page from object data - but the target object IS referenced in the url.
** How To:**
To make this work, we need to pick out a variable from the URL of the current page and use it to generate the URL of the target page.
1. [urls.py] Models *Banker* and *Iaccount* are related as One-to-Many so if we are creating an *Iaccount* we cannot usually determine the *Banker* object to return to. In this example, URL1 contains the variable `iid` - the primary key to the Banker object; this will render a create form. We want to be able to reference URL2 from this form.
2. [views.py: get_initial] We can access the variable we want with `self.kwargs['iid']` and use it to set the initial value of the `ident` fields which links back to the *Banker* object
3. [views.py: get_success_url] In the same way we can pass the value into the form's *success_url* to point at URL2
4. [template] In the template, we can also access the variable now as `form.ident.value` so we can construct a *Go Back* link to URL2
Thanks to Thomas Orozco for leading the way.
Modelform cant inhertit from forms. To solve this issue, split thing you wanto to inherit into filed definition and functionality definition. For modelform use the base_fields.update method as mentioned in the code.
Use this snippet to list all errors in a form. The message will be shown in a boostrap-type alert which can be 'closed' using a dismiss button.
The **field label** and the **error** will be listed.
e.g.
> * Name: This field is required
> * Email: Please enter a valid email
Validate Ukraine telephone numbers in popular formats:
+380 XX XXX-XX-XX
0XX-XXX-XX-XX
(0XX) XXX-XX-XX
This snippet fixes the errors found in
http://djangosnippets.org/snippets/2579/
Validates and cleans UK telephone numbers. Number length is checked, and numbers are cleaned into a common format. For example, "+44 (0)1234 567890" will be stored as "01234 567890".
Can reject premium numbers (0912 312 3123) or service numbers (1471, 118 118) with UKPhoneNumberField(reject=('premium', 'service'))
Can reject multiple number types so you can tune the form input to accept only landline or only mobile, or whatever combination you want.
Corrects the errors found in http://djangosnippets.org/snippets/1207/ and adds extra functionality and detail to the code found at http://djangosnippets.org/snippets/2809/
In particular, this version rejects individual invalid area codes and caters for area codes with mixed-length numbering in fine-grained detail.
**Uses info from:** [here](http://www.aa-asterisk.org.uk/index.php/Regular_Expressions_for_Validating_and_Formatting_UK_Telephone_Numbers)