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
11
votes
5 answers

Django MultiValueField - How to pass choices to ChoiceField?

I have a multivaluefield with a charfield and choicefield. I need to pass choices to the choicefield constructor, however when I try to pass it into my custom multivaluefield I get an error __init__() got an unexpected keyword argument 'choices'. I…
Terry J
  • 1,051
  • 2
  • 10
  • 9
9
votes
2 answers

Django ImageField widget that accepts upload or external link as source

Is there a Django widget for ImageFields that provides the option to either upload an image or provide a url? I'm thinking like Summernote does (if you click the image icon) in its examples I've browsed through many apps and they are all focused on…
43Tesseracts
  • 4,617
  • 8
  • 48
  • 94
9
votes
2 answers

Django: How to check if there are field errors from within custom widget definition?

I'd like to create widgets that add specific classes to element markup when the associated field has errors. I'm having a hard time finding information on how to check whether a field has errors associated with it, from within widget definition…
bitbutter
  • 580
  • 5
  • 15
9
votes
2 answers

Django python: Using DateInput --Required argument 'year' (pos 1) not found

I'm trying to use DateInput, but I get the following error Required argument 'year' (pos 1) not found here {{ form.incident_date_time_reported|add_class:"form-control"}} (line 52) forms.py from django import forms import datetime from functools…
user1807271
  • 946
  • 3
  • 13
  • 32
8
votes
5 answers

Widget filling values in two fields

I know that if I need a custom "selector" for a field in django-admin I need to create a custom widget. But what if the widget have to produce two values, for example X and Y coordinates, how can I fill them in two different fields from the model?
Ilian Iliev
  • 3,217
  • 4
  • 26
  • 51
8
votes
4 answers

Django Float Field input

Which is the best way to create a django form field that accepts floats and has the same functionality as a NumberInput? What I mean with same functionality is that django's NumberInput has arrows right next to the input that can increase or…
Alejandro Veintimilla
  • 10,743
  • 23
  • 91
  • 180
8
votes
2 answers

How to display Django SelectDateWidget on one line using crispy forms

I am trying to display the 3 select fields that are rendered out using Django SelectDateWidget on one line. When I use crispy forms, they are all on separate rows. Is there a way to use the Layout helper to achieve this? Thank you! class…
7
votes
2 answers

How to set default date in SelectDateWidget in django

I am using SelectDateWidget widget for entering date in the form field. But I want it to show the current date by default. How can I do that? model.py bdate = models.DateField(default=datetime.date.today()) This is giving error. can anyone tell…
G Gill
  • 1,087
  • 1
  • 12
  • 24
7
votes
1 answer

Django admin GenericForeignKey widget

I'm creating a Django app where all the models can be related to each other in an order set by the user. I'm setting all this up using GenericForeignKeys. The kicker is that I need to be able to support multiple collections of these types of…
kayluhb
  • 648
  • 7
  • 21
7
votes
1 answer

How to add Widgets to UpdateView in Django

I need to add this widget to the django UpdateView, class tlistUpdate(LoginRequiredMixin,UpdateView): fields = ('title', 'thumbnail', 'content', 'tags') model = htmlpage template_name = 'blog/create_form.html' Tried adding widgets = { …
VA splash
  • 553
  • 1
  • 8
  • 25
7
votes
2 answers

Check for errors and other values at the widget level - maybe using custom form field

How can I access if a field has)errors at the level of widget? Using default I tried: {% if widget.attributes.has_errors %} or {% if widget.has_errors %} but are not working. I use custom widget templates, I'm thinking to use a custom form Field…
user3541631
  • 3,686
  • 8
  • 48
  • 115
7
votes
2 answers

How to add CSS class to widget/field with Django 1.11 template-based form rendering

Note: This question shouldn't be conflated with past questions that are similar but before Django 1.11, when they released template-based form rendering. I understand that Django now has template-based form rendering. From what I understand, this is…
Greg Schmit
  • 4,275
  • 2
  • 21
  • 36
7
votes
1 answer

JsonEditor integration with Django Admin

I am working on integrating JSONEditor into the Django admin. There is one field in my model that uses the Postgres JSON and the Tree editor in this library is perfect. models.py class Executable(models.Model): """ Simplified model for sake…
7
votes
2 answers

How to change ManyToManyField widget to CheckboxSelectMultiple without overriding field definition in a ModelForm

I have django ModelForm for model with ManyToManyField. I want to change widget for this field toCheckboxSelectMultiple. Can I do this without overriding a field in a form definition? I constantly use code similar to this: class MyModel(ModelForm): …
dzida
  • 8,854
  • 2
  • 36
  • 57
7
votes
4 answers

Django MultiWidget Phone Number Field

I want to create a field for phone number input that has 2 text fields (size 3, 3, and 4 respectively) with the common "(" ")" "-" delimiters. Below is my code for the field and the widget, I'm getting the following error when trying to iterate the…
1
2
3
21 22