Django Forms - How to change an attribute from a field?

dragosb

The Django forms functionality generates the html code for my form fields.

I would like to change some attributes or add some for a specific field from that form.

For example I would like to specify a class for a field.

How can I do that?

I read an answer on SO that I could do it like this:

city = forms.CharField(widget=forms.TextInput(attrs={'class':'anyclass'}))

but i get an error, that attrs does not exist or is undefined...

UPDATE

Here is my forms.py

from django import forms from HelloUprise.models import Goal from django.contrib.auth.models import User

class GoalForm(forms.ModelForm):
    deadline = forms.DateField(widget=forms.DateField(attrs={'class': 'form-control'}))


class Meta:
    model=Goal
    fields = ['goal', 'description', 'deadline', 'priority']

Here is the error:

TypeError at /
__init__() got an unexpected keyword argument 'attrs'

Traceback Switch to copy-and-paste view

C:\Python27\lib\site-packages\django\core\handlers\base.py in get_response resolver_match = resolver.resolve(request.path_info) ... ▶ Local vars C:\Python27\lib\site-packages\django\core\urlresolvers.py in resolve for pattern in self.url_patterns: ... ▶ Local vars C:\Python27\lib\site-packages\django\core\urlresolvers.py in url_patterns patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module) ... ▶ Local vars C:\Python27\lib\site-packages\django\core\urlresolvers.py in urlconf_module self._urlconf_module = import_module(self.urlconf_name) ... ▶ Local vars C:\Python27\lib\site-packages\django\utils\importlib.py in import_module import(name) ... ▶ Local vars C:\Users\Barosan\Documents\GitHub\uprisedev\UpriseApplication\UpriseApplication\urls.py in url(r'^', include('HelloUprise.urls')), ... ▶ Local vars C:\Python27\lib\site-packages\django\conf\urls__init__.py in include urlconf_module = import_module(urlconf_module) ... ▶ Local vars C:\Python27\lib\site-packages\django\utils\importlib.py in import_module import(name) ... ▶ Local vars C:\Users\Barosan\Documents\GitHub\uprisedev\UpriseApplication\HelloUprise\urls.py in from HelloUprise import views ... ▶ Local vars C:\Users\Barosan\Documents\GitHub\uprisedev\UpriseApplication\HelloUprise\views.py in from HelloUprise.forms import GoalForm ... ▶ Local vars C:\Users\Barosan\Documents\GitHub\uprisedev\UpriseApplication\HelloUprise\forms.py in class GoalForm(forms.ModelForm): ... ▶ Local vars C:\Users\Barosan\Documents\GitHub\uprisedev\UpriseApplication\HelloUprise\forms.py in GoalForm deadline = forms.DateField(widget=forms.DateField(attrs={'class': 'form-control'})) ... ▶ Local vars C:\Python27\lib\site-packages\django\forms\fields.py in init super(BaseTemporalField, self).init(*args, **kwargs) ... ▶ Local vars

Daniel Roseman

You haven't done what you showed in the example. Instead of using a widget, you're trying to pass another instance of the field. That doesn't work, as the error shows.

It should be:

deadline = forms.DateField(widget=forms.DateInput(attrs={'class': 'form-control'}))

note the use of DateInput instead of DateField.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Django Model forms: how change html of a field

change field type in django forms

How to change the color of field taking attribute from the field

Python django change field attribute

How to add a search field for Django Admin Change/Add forms?

How to change existing attribute in a field

Django - Forms - change field names on template

Django forms adding class attribute from the constructor

In Django, how to extract ID attribute from html Input tag, WITHOUT using django forms

django crispy forms - 'SubWidget' object has no attribute 'field'

how to change models field value from views in django

Django forms - how to override field validation

How to clear an image field in django forms?

How to solve 'This field is required' for django forms

How to customize Date and Time field in django forms?

Django hide field from forms and add automatically field

How to add a form as field attribute in a django ModelForm

Django - How to change value of forms.ModelChoiceField

How can I remove "multiple" attribute from django_filters form select field?

how can i get all attribute data from ManyToMany field Django?

How to pass field attribute value to Django form field error message

How to change the "from" field in nodemailer?

How change From field in Postfix

Django change database field from integer to CharField

how to change a char field to list field or array field in Django model?

How to make custom field from django field

how can i update ManyToManyField child attribute in Django forms?

Django: How to change field name in nested serializer

Django Admin how to change text in relations field