Hi there, New to Django and coding. I’m having issues with crispy forms when trying to load a view with a form
views.py
def job_information(request, jobid):
task = get_object_or_404(jobs, jobid=jobid)
if request.method == "POST":
form = job_form(request.POST, instance=task)
if form.is_valid():
form.save()
return redirect('activities')
else:
form = job_form(instance=task)
return render(request, 'workinfo.html', {'form': form})
forms.py
class job_form(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(job_form, self).__init__(*args, **kwargs)
# If you pass FormHelper constructor a form instance
# It builds a default layout with all its fields
self.helper = FormHelper()
self.helper.form_id = 'id-job_form'
self.helper.form_method = 'post'
self.helper.form_action = 'submit_survey'
self.helper.form_tag = False
# You can dynamically adjust your layout
self.helper.add_input(Submit('save', 'Save'))
workinfo.html
{% extends 'base.html'%}
{% load crispy_forms_tags %}
{% block body%}
<div class="container">
<form action="{% url 'activities' %}" class="uniForm" method="post">
<br>
{% csrf_token %}
{% crispy job_form job_form.helper %}
</form>
</div>
{% endblock %}
when I use {% crispy job_form job_form.helper %}
i get a VariableDoesNotExist error however when I just use {% crispy form %}
it works fine but I want to also add a couple of inline forms and so I won’t be able to use {% crispy form %}
. As using {% crispy form %}
works fine I am lead to believe the issue is somehow in that crispy form tag and from reading the crispy forms documentation {% crispy job_form job_form.helper %}
should work. I’m not sure why I am being thrown an error. Any help would be greatly appreciated.