Questions tagged [django-widget]

Django widgets handle the rendering of the HTML for form fields

A widget is Django’s representation of a HTML input element. The widget handles the rendering of the HTML, and the extraction of data from a GET/POST dictionary that corresponds to the widget. -- https://docs.djangoproject.com/en/dev/ref/forms/widgets/

Django includes a number of built in widgets, including TextInput, RadioSelect and CheckboxInput. It is easy to change the way a form field is displayed by declaring the widget when you define your Django form.

For example, if you have a form ChoiceField, where the user can choose from a list of options, your choice of widget determines whether this is rendered as a dropdown menu, or a list of radio buttons.

If custom behavior is required, Django's built in widgets can be subclassed and extended.

329 questions
3
votes
3 answers

How to make a date picker that does not select previous dates in Django

I want to make a date picker that does not select previous dates using Django. class DateInput(forms.DateInput): input_type = 'date' class TimeInput(forms.TimeInput): input_type = 'time' """class used for booking a time slot.""" class…
3
votes
1 answer

How to set MaxDate and MinDate of a SelectDateWidget in Django?

How to set MaxDate and MinDate of a SelectDateWidget in Django? I am using that widget for a birthdate field. #found in: from django.forms.extras.widgets import SelectDateWidget from django import forms def someform(forms.Form): birthdate =…
Joseph Lafuente
  • 337
  • 3
  • 14
3
votes
3 answers

Django - Admin custom field display & behavior

Let's imagine this model: class ScribPart(models.Model): name = models.CharField(max_length=50, unique=True) text = models.TextField() I'd like to attach a specific class to the field text and call a specific js file. So that the admin page…
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
3
votes
1 answer

Django Media assets in Forms, how to defer/async JS assets

The official docs explain how to automatically adds assets for certain widgets., from thier example: from django import forms class CalendarWidget(forms.TextInput): class Media: css = { 'all': ('pretty.css',) } …
run_the_race
  • 1,344
  • 2
  • 36
  • 62
3
votes
2 answers

creating a croppable image field - how to store the widget's data?

I am wondering how to best implement a croppable ImageField in Django. Basically i'd like to upload images and crop them. For example using imagAreaSelect The first approach that came to my mind was to create an ImageField and some meta fields on my…
arie
  • 18,737
  • 5
  • 70
  • 76
3
votes
1 answer

How can i display my RadioSelect Widget in django

I have a model, form, view and i cannot seem to actually display the RadioSelect widget. it doesnt show anything, i think everything is fine except the views, however, i am unsure. I basically want someone to choose and option from the two radio…
JiM
  • 45
  • 3
3
votes
0 answers

Django: How to properly render SplitDateTimeWidget for DateTimeRangeField

Online material (docs, code) for django's (postgres-specific) range fields is lacking. I tried overriding the widget for DateTimeRangeField, but can't get it to show 2 pretty date-time inputs using SplitDateTimeWidget (start datetime, end datetime)…
shad0w_wa1k3r
  • 12,955
  • 8
  • 67
  • 90
3
votes
3 answers

How to add additional context to a django form widget

the basic Django CheckboxSelectMultiple widget allows for a label and value to be passed through to the template. I want to add 2 additional fields from a model but I cannot work out how, although I believe it is through subclassing get_context I…
HenryM
  • 5,557
  • 7
  • 49
  • 105
3
votes
1 answer

Implementing Django FilteredSelectMulitple into none admin form

I'm trying to implement the Django widget FilteredSelectMultiple into a non-admin form and although it displays, when loading I get a JavaScript error in console. The error is TypeError: node.tagName is undefined SelectFilter2.js:11:9 If I…
HenryM
  • 5,557
  • 7
  • 49
  • 105
3
votes
0 answers

Django template: How to split SplitDateTimeWidget?

Can't figure out how to render date and time fields of SplitDateTimeWidget separately. This renders both fields: {{ form.datetime_field }} But what if I want to use them on different places? I tried: {{ form.datetime_field_0 }}
{{…
Milano
  • 18,048
  • 37
  • 153
  • 353
3
votes
1 answer

How change html template of ImageField in edit form?

I use Django 1.11 in my project. In my edit form I have ImageField. Django by default render html which you can see below. How change html of ImageField in edit form correctly? I tried next code but Django raise error TemplateDoesNotExist. Django…
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
3
votes
1 answer

Django MultiValueField with non-required fields

I'm trying to make a MultiValueField for which the last field is not required. According to the docs, if I set require_all_fields to False, then I can set some fields to not be required, like this:- class MyMultiWidget(widgets.MultiWidget): def…
bodger
  • 1,112
  • 6
  • 24
3
votes
1 answer

Filter many-to-many multiple select field

I have a object (Book) with a many-to-many relation with another object (Category). 'Category' is used to render subcategories too. The diference between a category and a subcategory is that a category has 'category_parent=None', while a subcategory…
3
votes
2 answers

How to set different placeholders for DateFromToRangeFilter?

I am using django_filters in my project and one of them is a DateFromToRangeFilter. This filter generates two input fields in your templates, figuratively - 'From' and 'To'. There is no problem to set a similar attribute for these fields. For…
3
votes
3 answers

Django simple form "object has no attribute 'is_hidden'"

I'm trying to render a simple form with no db models but I am getting error. Following is the code: from django import forms from django.utils.safestring import mark_safe class ContactForm(forms.Form): category_options = ( ('select…
Shazia Nusrat
  • 174
  • 1
  • 10
  • 36