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
7
votes
3 answers

How can I override the 'type' attribute of a ModelForm in Django?

Specifically, I would like to render date widget in a form but I would like it to 'be' HTML5 (so I can just forget about the javascript or whatever and trust Chrome, Opera and Safari to display the datepicker). No javascript solutions please, I have…
pablete
  • 1,030
  • 1
  • 12
  • 21
6
votes
1 answer

Django - Show BooleanField in a formset as one group of radio buttons

I have the following models: class Profile(models.Model): verified = models.BooleanField(default=False) def primary_phone(self): return self.phone_set.get(primary=True) class Phone(models.Model): profile =…
Aziz Alfoudari
  • 5,193
  • 7
  • 37
  • 53
6
votes
2 answers

Is there any widgets available for `DurationField` in Django?

I am about to add DurationField 's widget for admin site and want duration field's widget for input. Problem statement In below PromoCode class have DurationField namely duration. But in admin it shows TextInput as input. class…
Devang Padhiyar
  • 3,427
  • 2
  • 22
  • 42
6
votes
1 answer

Django M2MFields 'through' widgets

Are there exists any examples of Django widgets which can be useful for ManyToManyFields with 'through' attributes? For example, i have these models (got the source from django documentation): from django.db import models class…
svfat
  • 3,273
  • 1
  • 15
  • 34
6
votes
2 answers

Django ModelForm with Select Widget - Use object.uid as default option value instead of object.id

I have a form inheriting from ModelForm as such: class ChildModel(ModelForm): class Meta: model = Documents fields = ('secretdocs') widgets = { 'secretdocs': Select(attrs={'class': 'select'}), …
limasxgoesto0
  • 4,555
  • 8
  • 31
  • 38
6
votes
0 answers

Add attributes to js media fields in django's widgets

I am using AMD loading with requirejs for most of my javascript but I have trouble using them in django's widgets. Django's documentation states that a media should be embedded in the following way: class Media: js = ('animations.js') This…
Filip Novotny
  • 397
  • 1
  • 4
  • 9
5
votes
3 answers

Django - CheckboxSelectMultiple without "------" choice

How can I remove "------" from rendered choices? I use in my model form: widgets = { 'event_form': forms.CheckboxSelectMultiple(), } In model I have IntegerField with choices: EVENT_FORM_CHOICES = ( (1, _(u'aaaa')), (2, _(u'bbbb')), …
tunarob
  • 2,768
  • 4
  • 31
  • 48
5
votes
1 answer

Customizing a Widget for ManyToMany field to a Model that have a circular ForeignKey to itself

I have two models Category and Products. a Product can have multiple Categories a Category can have multiple Products. Categories have a circular Foreign key, to itself. not all Categories have the same depth level Example: Category A Category…
user3541631
  • 3,686
  • 8
  • 48
  • 115
5
votes
2 answers

Django ManytoManyField and widgets

I have two models, Product and Category and ManytoMany field in Product. The Category appear as key on ProductCreate View. I need to customize the widget and field for Categories. I checked in Django source Fields and Widgets but I don't see a…
user3541631
  • 3,686
  • 8
  • 48
  • 115
5
votes
2 answers

How to render Form Choices manually

I have in form.py SELECT_PRODUCT = [ ('item1', 'item1'), ('item2', 'item2'), ('item3', 'item3'), ('item4', 'item4'), ] class OrderBonus(forms.Form): select_product = forms.CharField(widget=forms.Select(choices=SELECT_PRODUCT…
Hellbea
  • 289
  • 6
  • 14
5
votes
2 answers

Specify max and min in NumberInput widget

I have a form that is using NumberInput widget to accept a rating for a Song from the user. The number should be between 0-5. My Song model has a MaxValueValidator but I want the NumberInput widget to show options only from 0-5.
Darshan Chaudhary
  • 2,093
  • 3
  • 23
  • 42
5
votes
3 answers

Customize ClearableFileInput in django template

I've got a form with profile_picture=ImageField field set to the initial value. It's using ClearableFileInput widget. I need to customize the form in the template, so I can't simply use {{ form.profile_picture}}. How can I split field elements and…
warownia1
  • 2,771
  • 1
  • 22
  • 30
5
votes
3 answers

How do I pass a context variable from a view to a custom field/widget in a Django template?

I have defined a custom field and widget for a form that is being served by subclassing UpdateView. So, something like this: myapp/forms.py: from .form_fields import MyCustomField from .widgets import MyCustomWidget class…
3cheesewheel
  • 9,133
  • 9
  • 39
  • 59
5
votes
2 answers

Apply css class to forms.RadioSelect widget

I am using a ModelForm and trying to set a css class on a Django forms.RadioSelect widget like class Meta: model = models.MyModel fields = ('rating',) widgets = { 'rating': forms.RadioSelect(attrs={'class':'star'}), } but…
mzu
  • 759
  • 8
  • 20
4
votes
2 answers

Django - Specifying default attr for Custom widget

I have created this widget class DateTimeWidget(forms.TextInput): attr = {'class': 'datetimepicker'} class Media: js = ('js/jquery-ui-timepicker-addon.js',) Then I use it on my form class SessionForm(forms.ModelForm): class Meta: …
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
1 2
3
21 22