How to set css class of a label in a django form declaration?

Widgets have an attrs keyword argument that take a dict which can define attributes for the input element that it renders. Forms also have some attributes you can define to change how Django displays your form. Take the following example: class MyForm(forms.Form): error_css_class=”error” required_css_class=”required” my_field = forms.CharField(max_length=10, widget=forms.TextInput(attrs={‘id’: ‘my_field’, ‘class’: ‘my_class’})) This works on any … Read more

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

You can declare an attribute on your ModelForm class, called formfield_callback. This should be a function which takes a Django model Field instance as an argument, and returns a form Field instance to represent it in the form. Then all you have to do is look to see if the model field passed in is … Read more

Django templates: verbose version of a choice

In Django templates you can use the “get_FOO_display()” method, that will return the readable alias for the field, where ‘FOO’ is the name of the field. Note: in case the standard FormPreview templates are not using it, then you can always provide your own templates for that form, which will contain something like {{ form.get_meal_display … Read more

Django: adding an “Add new” button for a ForeignKey in a ModelForm

I have solved it in a custom widget. I don’t remember if I took parts from Django admin, or I have built from scratch. So the form will be: class OrderNewForm(forms.ModelForm): client = forms.ModelChoiceField( required=False, queryset=Client.objects.all(), widget=RelatedFieldWidgetCanAdd(Client, related_url=”so_client_add”) ) class Meta: model = Order fields = (‘code’, ‘client’) And the widget, that renders the “+” … Read more

Django form with choices but also with freetext option?

I would recommend a custom Widget approach, HTML5 allows you to have a free text input with a dropdown list which would work as a pick-one-or-write-other type of field, this is how I made it: fields.py from django import forms class ListTextWidget(forms.TextInput): def __init__(self, data_list, name, *args, **kwargs): super(ListTextWidget, self).__init__(*args, **kwargs) self._name = name self._list … Read more

Get type of Django form widget from within template

Making a template tag might work? Something like field.field.widget|widget_type Edit from Oli: Good point! I just wrote a filter: from django import template register = template.Library() @register.filter(‘klass’) def klass(ob): return ob.__class__.__name__ And now {{ object|klass }} renders correctly. Now I’ve just got to figure out how to use that inside a template’s if statement. Edit … Read more

Customize/remove Django select box blank option

Haven’t tested this, but based on reading Django’s code here and here I believe it should work: class ThingForm(forms.ModelForm): class Meta: model = Thing def __init__(self, *args, **kwargs): super(ThingForm, self).__init__(*args, **kwargs) self.fields[‘verb’].empty_label = None EDIT: This is documented, though you wouldn’t necessarily know to look for ModelChoiceField if you’re working with an auto-generated ModelForm. EDIT: … Read more