Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Why does my CharField not get given the class vTextField?

I have a form like this:

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

Which generates this HTML:

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

This shows up as really narrow in the admin (where I've got a custom view using this form).

If I have a model defined like this:

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's the most elegant way of getting the class vTextField added to my form? That class seems to be the way normal text inputs are styled, so I'd like to use that, rather than styling it myself.

like image 526
Dominic Rodger Avatar asked Jan 22 '23 19:01

Dominic Rodger


1 Answers

Whilst @czarchaic's answer worked (so +1), browsing the source yielded this solution, which I prefer:

from django.contrib.admin.widgets import AdminTextInputWidget

class DiaryEventForm(forms.Form):
    title = forms.CharField(max_length = 200, widget = AdminTextInputWidget())
like image 180
Dominic Rodger Avatar answered Feb 13 '23 22:02

Dominic Rodger