Django: Why is my CharField not getting the vTextField class?

I have a form like this:

class DiaryEventForm(forms.Form):
  title = forms.CharField(max_length = 200)

What generates this HTML:

<input id="id_title" type="text" name="title" maxlength="200" /> 

This displays as very narrow in the admin (where I have a custom view using this form).

If I have a model defined as follows:

class DiaryEvent(BaseModel):
  title = models.CharField(max_length = 200)

I get this HTML:

<input id="id_title" type="text" class="vTextField" name="title" maxlength="200" />

What is the most elegant way to add a class vTextFieldto my form? This class seems to be a way of typing text, so I would like to use it, not the style itself.

+3
source share
3 answers

While @czarchaic's answer worked (like +1), looking at the source gave this solution, which I prefer:

from django.contrib.admin.widgets import AdminTextInputWidget

class DiaryEventForm(forms.Form):
    title = forms.CharField(max_length = 200, widget = AdminTextInputWidget())
+5
source

 class DiaryEventForm(forms.Form):
  title = forms.CharField(max_length = 200, widget=forms.TextInput(attrs={'class': 'vTextField'}))
+3

.

div :

<div class="vTextField">
<label>
    {{ form.title }}
</label>
</div>

css:

.vTextField label{
 your style here
}

In this case you do not need to specify a style class in python

-2
source

Source: https://habr.com/ru/post/1727971/


All Articles