Suhas
Suhas

Reputation: 313

How to Style The Django Forms?

So, I am making a project, and I need a couple of settings, in the form of BooleanFields.

so,

class SettingsForm(forms.ModelForm):
    notifications = forms.BooleanField(label="", required=False)
    email_notifications = forms.BooleanField(label="", required=False)
    restricted_mode = forms.BooleanField(label="", required=False)
    hide_subscriber_count = forms.BooleanField(label="", required=False)
    private_channel = forms.BooleanField(label="", required=False)
    playlists_private = forms.BooleanField(label="", required=False)

    class Meta:
        model = Settings
        fields = ['notifications', 'email_notifications', 'restricted_mode', 'hide_subscriber_count', 'private_channel', 'playlists_private']

How to use widget=forms.BooleanField(attrs={"id":"notifications", "class":"notifications"})? How to stick an id to the notifications field or email_notifications field?

Upvotes: 0

Views: 104

Answers (1)

ha-neul
ha-neul

Reputation: 3258

In your forms.py, try this:


class SettingsForm(forms.ModelForm):

    class Meta:
        model = Settings
        fields= ('notifications', 'email_notifications', 'restricted_mode', 'hide_subscriber_count', 'private_channel', 'playlists_private')
        widgets = {
            'notifications': forms.RadioSelect(attrs= {
                 'id': 'notifications'}),
            'email_notifications': forms.RadioSelect(attrs= {
                 'class': 'email_notifications'})
        }

Upvotes: 1

Related Questions