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
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.
Comments