Reputation: 31
I trying to use crispy layout for my form. I have followed instruction and tutorial but because I am new to Django and python I got lost and it does not render my form as well as submit button.
I have model form defined:
class nowyPhForm(forms.ModelForm):
class Meta:
model = Phandlowy
fields = ('imie', 'nazwisko', 'email', 'telefon', 'firma', 'filia')
def __init__(self, *args, **kwargs):
super(nowyPhform, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_method = 'post'
self.helper.form_action = 'submit_survey'
self.helper.add_input(Submit('submit', 'Submit'))
self.helper.layout = Layout(
Row(
Column('imie', css_class='form-group col-md-6 mb-0'),
Column('nawisko', css_class='form-group col-md-6 mb-0'),
css_class='form-row'
),
Row(
Column('email', css_class='form-group col-md-6 mb-0'),
Column('telefon', css_class='form-group col-md-6 mb-0'),
css_class='form-row'
),
Row(
Column('firma', css_class='form-group col-md-6 mb-0'),
Column('filia', css_class='form-group col-md-6 mb-0'),
css_class='form-row'
),
Submit('submit', 'Sign in')
)
and HTML
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
{% crispy form %}
</div>
{% endblock %}
I would appreciate if someone could spot problem.
Upvotes: 0
Views: 1527
Reputation: 191
This worked for me (in template.html):
{% crispy form form.helper %}
Upvotes: 0
Reputation: 820
You can add the submit button manually as follows. Following snippet will render your form.
views.py
from django.shortcuts import render, redirect
from .forms import nowyPhForm
def view(request):
form = nowyPhForm()
if request.method == 'POST':
form = nowyPhForm(request.POST)
if form.is_valid():
form.save()
return redirect('some_other_url')
return render(request,'template.html',{'form':form})
template.html
{% load crispy_forms_tags %}
{% block content %}
<div class="container">
<form method="POST">
{% csrf_token %}
{{ form | crispy }}
<button type="submit">Submit</button>
</form>
</div>
{% endblock %}
Upvotes: 1