0

I want to add class for my file field as other elements(while I can't use attrs={"rows": "1", "class": "form-control"} I've no idea how to go on), couldn't find any guides about that every where they applied that on other fields. forms.py

from django import forms
from .models import MedicalRecords

class UpdateMedicalRecordForm(forms.ModelForm):
    class Meta:
        model = MedicalRecords
        fields = ("title", "file", "doctor")

        widgets = {
            "title": forms.Textarea(attrs={"rows": "", "class": "form-control"}),
             "file": ?? (how to add class as above),
        }
1
  • Style admin panel ? is that what you mean? Commented Jan 27, 2021 at 13:58

1 Answer 1

1

As per Django widget classes (see this link for available types of widget/html input types):

from django import forms
from .models import MedicalRecords

class UpdateMedicalRecordForm(forms.ModelForm):
    # we can directly specify attributes to individual fields like this
    title = forms.CharField(max_length = 100, widget = forms.TextInput(attrs={'class':'title_class_name', 'id':'title_id'}))
    file = forms.ImageField(widget = forms.FileInput(attrs={'class':'file_class_name', 'id':'file_id'}))
    
    class Meta:
        model = MedicalRecords
        fields = ("title", "file", "doctor")
        
        # or we can use widgets like this
        widgets = {
            "title": forms.Textarea(attrs={"rows": "", "class": "title_class_name"}),
             "file": forms.FileInput(attrs={"rows": "", "class": "file_class_name"}),
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.