I am trying to use Crispy Forms
to make my forms look good. I have the following in my forms.py
:
from django import forms
from .models import Team
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout, Field
from crispy_forms.bootstrap import (
PrependedText, PrependedAppendedText, FormActions)
class CreateTeamForm(forms.ModelForm):
class Meta:
model = Team
fields = [ 'Project_name', 'Project_number'
]
helper = FormHelper()
helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
helper.form_method = 'POST'
Then in my views.py
:
def create_team(request):
if request.method == 'POST':
form = CreateTeamForm(request.POST, request.FILES)
if form.is_valid():
form.save()
return render('/teams/my_team.html',{''})
else:
form = CreateTeamForm()
return render(request, 'teams/team_form.html', {'form':CreateTeamForm()})
And finally in my template:
{% extends "main/base.html" %}
{% load crispy_forms_tags %}
{% block content %}
<div class="row">
<div class="jumbotron">
{% crispy form %}
</div>
</div>
{% endblock %}
However, my submit button that I called isn't being displayed. I have read the cripsy form documentation and I can't seem to find anything wrong with my implementation. Everything except for the submit button is displayed. Any ideas?
Your FormHelper
should be an attribute of your form class, not its Meta
class. Outdent that part of your code:
class CreateTeamForm(forms.ModelForm):
class Meta:
model = Team
fields = [ 'Project_name', 'Project_number' ]
helper = FormHelper()
helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
helper.form_method = 'POST'
This solution worked for me
from django.forms import ModelForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
class BillForm(ModelForm):
def __init__(self, *args, **kwargs):
super(BillForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.add_input(Submit('submit', 'Submit', css_class='btn-primary'))
self.helper.form_method = 'POST'
class Meta:
model = Bill
fields = ('__all__')
you define your helper inside __init__
method following this exemple and then you put this in your template:
{% load crispy_forms_tags %}
{% crispy form %}
and in your view/action you should have something like that
form = BillForm()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With