1

I'm using Django 1.11.9 on Ubuntu, with an app structure as such:

VisitorTrackingApp
|
 ---> admin.py
 ---> forms.py
 ---> models.py
 ---> static
      |
       ---> textboxes.css
 ---> templates
      |
       ---> index.html
       ---> signin.html
 ---> views.py
 ---> widgets.py

In my models.py I've defined a model Visitors with an attribute Company (among many others):

class Visitors(models.Model):
    Company = models.CharField(max_length = 50, blank = True)

In widgets.py I've defined a custom widget like:

from django import forms

class TextWidget(forms.TextInput):
    class Media:
        css = {'all': ('textboxes.css',)}

Then I put it all together in forms.py:

from django.forms import ModelForm

from models import Visitor
from widgets import TextWidget

class VisitorsForm(ModelForm):
    class Meta:
        model   = Visitors
        fields  = ('VisitorType', 'FirstName', 'LastName', 'Company', 'Phone', 'Email')
        widgets = {'Company': TextWidget(),}

All of this executes without error. But my understanding based on the Django documentation here and here is that because I have widgets = {'Company': TextWidget(),}, then Company should render according to the CSS in textboxes.css, but it is not. When I do a sanity check like this:

t = TextWidget()
print >>sys.stderr, t.media

and look at my Apache log, I find that my custom widget does indeed have the correct path to my CSS file!

The post at Django widget override template sort of gets at what I'm asking, but he seems to be using an HTML template to define his custom widget? Not sure what's going on here, any help is appreciated. Thank you!

Pat Jones
  • 876
  • 8
  • 18

0 Answers0