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>
- [Answer]-Django Admin Error. "Could not import my_app.views.admin_user"
- [Answer]-Error when update password in extending user models. Why and how to fix it ?
- [Answer]-How to get actual value from HttpResponse in view?
- [Answer]-Running commands with django-extensions
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'}))
- [Answer]-Django View Testing Assertion Error
- [Answer]-Order by float field in Django not working
- [Answer]-How to submit POST request to '"current_url"/submit' in HTML's form action using Django templates?
- [Answer]-Django admin: Adding a textarea and a button that invokes a function
- [Answer]-Why do my CBV forms have no fields?
Source:stackexchange.com