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

Django: How do I add arbitrary html attributes to input fields on a form?

I have an input field that is rendered with a template like so:
{{ form.city }}
Which is rendered as:
Now suppose I want to…
User
  • 62,498
  • 72
  • 186
  • 247
77
votes
5 answers

Django Admin: Using a custom widget for only one model field

I have a DateTimeField field in my model. I wanted to display it as a checkbox widget in the Django admin site. To do this, I created a custom form widget. However, I do not know how to use my custom widget for only this one field. The Django…
Belmin Fernandez
  • 8,307
  • 9
  • 51
  • 75
51
votes
1 answer

How do I use Django's MultiWidget?

The documentation is a bit lacking with respect to this feature. from django import forms class TwoInputWidget(forms.MultiWidget): """An example widget which concatenates two text inputs with a space""" def __init__(self, attrs=None): …
John Mee
  • 50,179
  • 34
  • 152
  • 186
43
votes
6 answers

How do you change the default widget for all Django date fields in a ModelForm?

Given a set of typical models: # Application A from django.db import models class TypicalModelA(models.Model): the_date = models.DateField() # Application B from django.db import models class TypicalModelB(models.Model): another_date =…
Brian M. Hunt
  • 81,008
  • 74
  • 230
  • 343
38
votes
7 answers

Filter ManyToMany box in Django Admin

I have an object with a ManyToMany relation with another object. In the Django Admin this results in a very long list in a multiple select box. I'd like to filter the ManyToMany relation so I only fetch Categories that are available in the City that…
schmilblick
  • 1,917
  • 1
  • 18
  • 25
37
votes
4 answers

Django: Admin: changing the widget of the field in Admin

I have a model with a boolean value like that: class TagCat(models.Model): by_admin = models.BooleanField(default=True) This appears as a checkbox in admin. How could I use this as a radio button in admin? Also, how do I make it be always…
mgPePe
  • 5,677
  • 12
  • 52
  • 85
24
votes
16 answers

Django: Country drop down list?

I have a form for address information. One of the fields is for the address country. Currently this is just a textbox. I would like a drop down list (of ISO 3166 countries) for this. I'm a django newbie so I haven't even used a Django Select…
User
  • 62,498
  • 72
  • 186
  • 247
16
votes
1 answer

Django subclassing multiwidget - reconstructing date on post using custom multiwidget

So my django book is back at university and I'm struggling to work this one out. I've subclassed django.forms.widgets.MultiWidget like so: class DateSelectorWidget(widgets.MultiWidget): def __init__(self, attrs=None, dt=None, mode=0): …
user257111
14
votes
2 answers

Django admin custom ArrayField widget

The current admin widget for ArrayField is one field, with comma as delimiter, like this (text list): This isn't ideal because I would have longer texts (even 20 words) and contain commas. I could change the delimiter to be something else but that…
Bojan Kogoj
  • 5,321
  • 3
  • 35
  • 57
14
votes
2 answers

django admin how to display widget on readonly field

I want to display my widget on the field at django admin when the field is readonly. admin.py class AudioTrackAdminInline(admin.StackedInline): model = AudioTrack form = AudioTrackForm readonly_fields = ('file',) forms.py class…
Feanor
  • 3,568
  • 5
  • 29
  • 49
13
votes
2 answers

How to use jQuery UI Datepicker as a Django Widget?

Some of my Django 1.3 models have DateField properties. When a form is generated, I'd like to use the jQuery UI Datepicker instead of a plain text field. I understand that I could create new widgets but I don't understand how. In addition, I am not…
Martin
  • 39,309
  • 62
  • 192
  • 278
12
votes
3 answers

Django Widget Media doesn't work

I need a widget, which should only display a block, because i will add functionality with jQuery. I am trying to include the javascript and stylesheet trough the "Media" class of Widget and it doesn't work for me. Here is the code: class…
Xidobix
  • 2,538
  • 3
  • 17
  • 10
11
votes
1 answer

Override Django widgets default templates

I want to override the Django (2.01) widget templates, because I need to add classes for each input, label, and position them differently app - templates - app - django - forms - widgets - input.html or app …
user3541631
  • 3,686
  • 8
  • 48
  • 115
11
votes
4 answers

Django SelectDateWidget to show month and year only

Is there a different widget or argument that will allow django to only show/take the year and month input instead of year, month and day? Currently using SelectDateWidget.
ApPeL
  • 4,801
  • 9
  • 47
  • 84
11
votes
3 answers

How to disable resize textarea in django?

I'm trying to disable resize to the textarea widget in django, this is my form: class VForm(forms.ModelForm): class Meta: model = Visions widgets = {'vision': forms.Textarea(attrs={'rows':6, …
Marco Herrarte
  • 1,540
  • 5
  • 21
  • 42
1
2 3
21 22