VariableDoesNotExist error with multiple crispy forms

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.

It’s not to do with crispy. You’re passing the form to your template as the context variable form:

return render(request, 'workinfo.html', {'form': form})

Therefore at all times you shoudl refer to form, not job_form.

Another hint - call the form class JobForm. It’s conventional in Python to use “CapWords” for classes as per PEP8 : https://www.python.org/dev/peps/pep-0008/#class-names . This can help avoid the confusion you had here, between the class and the instance of it.

Hope that helps!

Dude! that has been bothering me for weeks. thankyou! Just to clarify, the issue was that I was calling job_form when the named context variable was ‘form’

return render(request, 'workinfo.html', {'form': form})

if I want to do multiple forms I can do the following?

views.py

return render(request, 'workinfo.html', 
{'JobForm': form1},
{'SupplementForm': form2},
...etc)

workinfo.html

{% crispy jobForm jobForm.helper %}
{% crispy SupplementForm SupplementForm.helper %}

Yes but for Pythonic variable names you would use lower_case_with_underscores (again, see PEP8):

job_form = JobForm(...)
supplement_form = SupplementForm(...)
return render(request, 'workinfo.html', 
{'job_form': job_form},
{'supplement_form': supplement_form})