[Answer]-Adding bootstrap or sylilng to django built in forms

1👍

You’re looking for Widgets!

From the documentation:

from django import forms

class CommentForm(forms.Form):
    name = forms.CharField()
    url = forms.URLField()
    comment = forms.CharField(widget=forms.Textarea)

In your example:

You’re using Bootstrap and therefore a simple css class (form-control) in each field will make it look great:

class Message_form(ModelForm):
    class Meta:
        model = Message
        fields = ['MessageSubject', 'MessageContent', 'MessageType']
        widgets = {'MessageSubject': forms.TextInput(attrs={'class': 'form-control'}),
                   'MessageContent': forms.Textarea(attrs={'class': 'form-control'}),
                   'MessageType': forms.TextInput(attrs={'class': 'form-control'}),}

Another thing, you don’t need to explicit define MessageID. All Django models have a primary key like the one you defined (check the docs).

0👍

Use Django Bootstrap3 app. It provides form fields and template tags for displaying horizontal and vertical bootstrap forms:

{% load bootstrap3 %}

<form action="." method="post" class="form">
    {% csrf_token %}
    {% bootstrap_form form %}
    <button type="submit" class="btn btn-primary">Submit</button>
</form>

0👍

I’ve been using crispy_forms and it’s working very well with bootstrap.

pip install django-crispy-forms

In your template:

{% load crispy_forms_tags %}
<form method="POST">
    {% csrf_token %}
    {{ form|crispy }}
</form>
👤nima

0👍

In template:

{{ form|as_p}}

in forms.py:

select = forms.CharField(widget=forms.Select(choices=select_choices, attrs={'class ':'form-control'}))

name = forms.CharField(widget=forms.TextInput(attrs={'class ':'form-control'}))

Leave a comment