DurationField has one optional argument:
input_formats : — Accepts any format understandable parse_duration () .
Expect data in the format "DD HH: MM: SS.uuuuuu" or in accordance with the ISO 8601 standard (for example, P4DT1H15M20S, which is equivalent to 4 1:15:20) or PostgreSQL daily interval format (for example, 3 days 04:05:06).
field_name = forms.DurationField ( ** options )
Illustration of a DurationField using an example. Consider a project named
pythonengineering that has an application named
Refer to the following articles to check how to create a project and an app in Django.
Enter the following code into the
forms.py file of the geeks application.
Add geeks app in
Now to to convert this form to a submission, we need a submission and a URL associated with this URL. Let's first create a view in the
views.py of the geeks app,
Here we import this particular form from forms.py and instantiate it in the view so that it could be displayed in the template.
Now, to initiate your Django form, you need to create a home.html where you can create things as you see fit. Let's create a form in
Finally, the URL to map to this view in urls.py
Let's start the server and check what's on actually happened, Run
Python manage.py runserver
Thus, DurationField is used to enter a specific duration into the database. You can enter the date of dispatch, date of birth, etc. So far we have discussed how to implement DurationField, but how to use it in the view to execute the boolean part. To do some logic, we need to get the value entered in the field into a Python string instance.
geeks_field is generated by replacing "_" with "". This is a field for entering the duration as received by the Python timedelta function from the user.
How to use DurationField?
DurationField is used to enter a specific duration into the database. You can enter the date of dispatch, date of birth, etc. So far we have discussed how to implement DurationField, but how to use it in the view to execute the boolean part. To do some logic, we need to get the value entered in the field into a Python string instance.
Let's try something other than the duration in the DurationField.
So it only accepts duration input otherwise validation errors will be seen. Now let's try to enter a valid duration in the field.
Date data can be retrieved using the appropriate query dictionary. If the method is GET, the data will be available in request.GET, and if post, then request.POST , respectively. In the above example, we have a temp value that we can use for any purpose.
Basic Field Arguments — they are arguments given to each field to apply some kind of constraint or to convey a particular characteristic to a particular field. For example, adding the
required = False argument to the DurationField will allow the user to leave it blank. Each Field constructor takes at least these arguments. Some Field classes accept additional field-specific arguments, but you should always accept the following:
|required|| By default, each Field class assumes the value is required, so to make it not required you need to set |
|label||The label argument lets you specify the “ human-friendly ”label for this field. This is used when the Field is displayed in a Form.|
|label_suffix||The label_suffix argument lets you override the form's label_suffix on a per-field basis.|
|widget||The widget argument lets you specify a Widget class to use when rendering this Field. See Widgets for more information.|
|help_text||The help_text argument lets you specify descriptive text for this Field. If you provide help_text, it will be displayed next to the Field when the Field is rendered by one of the convenience Form methods.|
|error_messages||The error_messages argument lets you override the default messages that the field will raise. Pass in a dictionary with keys matching the error messages you want to override.|
|validators||The validators argument lets you provide a list of validation functions for this field.|
|localize||The localize argument enables the localization of form data input, as well as the rendered output.|
|disabled||The disabled boolean argument, when set to True, disables a form field using the disabled HTML attribute so that it won't be editable by users.tr>|