django-widget

Django: How to add dynamic classes to select options in a form

邮差的信 提交于 2019-12-13 04:52:40
问题 I have a ModelForm that I'm rendering in the template with django-widget-tweaks. I have a ChoiceField select that prints out something like this: <select> <option value="object.id">object.name</option> </select> But for Javascript manipulation purposes, I need the following: <select> <option value="object.id" class="object.otherPropery">object.name</option> </select> django-widget-tweaks has an add_class function, but it only adds a class to the select box, not the contained options. I have

Instance construction fails on form data check

安稳与你 提交于 2019-12-13 03:46:49
问题 I have a custom Django 1.11 Widget that allows for a dynamic number of key/value pairs to be entered into a form, which are assembled into a dict by the widget's value_from_datadict method. When I check the form's cleaned_data , I see exactly what I'm expecting: {'fieldname': {'key1': 'value1', ....} However, when the form is saved, fieldname isn't being updated. I've traced this down to the implementation of django.forms.models.construct_instance : def construct_instance(form, instance,

How to set default date in SelectDateWidget in django

瘦欲@ 提交于 2019-12-12 10:49:12
问题 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 the correct way to do that? Also, my in template {{ form.bdate }} when I use the above mentioned line in my template it displays like this -- -- -- but I want something like this month date year . how can i do this? My form is: widgets = { 'bdate' :

Date format for django's SelectDateWidget

本秂侑毒 提交于 2019-12-12 08:58:47
问题 SelectDateWidget is very convenient but it normally seems to return dates in the format "%Y-%m-%d". It doesn't take a format parameter and doesn't have much documentation. Anyone run into this or have an idea how to work around it to get the output of the date into another format? There is this ticket, #6231 saying that there's a fix to have it use a DATE_FORMAT setting from settings.py (though I don't see why it can't just be given a format parameter also). There's a patch in the ticket from

How to add default value to django-markitup widget?

浪尽此生 提交于 2019-12-12 02:47:35
问题 Friends,I want to add default value to django MarkItUpWidget title = forms.CharField(max_length = 30) content = forms.CharField(widget=MarkItUpWidget()) 回答1: The way to provide an initial value for unbound forms in Django is the initial argument. content = forms.CharField(widget=MarkItUpWidget(), initial='initial value') 回答2: set the value in the form definition: q = forms.CharField(label='search' widget=forms.TextInput(), initial=123) 来源: https://stackoverflow.com/questions/9849975/how-to

In django forms custom widget return list as value instead of string

吃可爱长大的小学妹 提交于 2019-12-11 09:19:39
问题 I am writting a custom widget which I want to return a list as the value. From what I can find to set the value that is returned you create a custom value_from_datadict function. I have done this def value_from_datadict(self, data, files, name): value = data.get(name, None) if value: # split the sting up so that we have a list of nodes tag_list = value.strip(',').split(',') retVal = [] # loop through nodes for node in tag_list: # node string should be in the form: node_id-uuid strVal = str

How to change year options of django's selectDateWidget

与世无争的帅哥 提交于 2019-12-11 02:24:18
问题 I want to use django's selectDateWidget for a form but the options for years are from 2012-2021. I need to display past years and probably up to 1980. How can I modify selectDateWidget? lessTime = forms.DateField(required=False, widget=SelectDateWidget()) 回答1: Pass a years argument: lessTime = forms.DateField(required=False, widget=SelectDateWidget(years=range(1980, 2012))) 来源: https://stackoverflow.com/questions/12187671/how-to-change-year-options-of-djangos-selectdatewidget

Specify Widget's class

為{幸葍}努か 提交于 2019-12-11 00:23:06
问题 I have a custom form with a field using a custom widget. I need to set the class of the widget, and currently I can only do it like this: class DesignCampaignForm(ModelForm): brand_logo = FileField(widget=ImagePreviewWidget) brand_logo.widget.attrs['class'] = 'image-preview' This means that for each widget declared like this I need two lines, and to repeat the class all the time - not very DRY. Is there a way to specify the widget class in the widget itself? I have been unable to find that in

How to display Django SelectDateWidget on one line using crispy forms

亡梦爱人 提交于 2019-12-10 14:08:40
问题 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 WineAddForm(forms.ModelForm): hold_until = forms.DateField(widget=SelectDateWidget(years=range(1950, datetime.date.today().year+50)), required=False) drink_before = forms.DateField(widget=SelectDateWidget(years=range(1950, datetime.date.today().year+50)),

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

有些话、适合烂在心里 提交于 2019-12-10 13:27:22
问题 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 and overwrite the default field. I know clean method exist but I don't know how to push to the widget the dynamic(non default) data/attributes I want. I tried: class AWidget(forms.Widget): def get_context(self, name, value, attrs): context = super()