Home > Net >  crispy_forms.exceptions.CrispyError: |as_crispy_field got passed an invalid or inexistent field - mo
crispy_forms.exceptions.CrispyError: |as_crispy_field got passed an invalid or inexistent field - mo

Time:03-06

I'm trying to create a frontend data entry page for an existing model. However, when clicking the link, I get an error:

crispy_forms.exceptions.CrispyError: |as_crispy_field got passed an invalid or inexistent field

Just to be clear, adding the data from Django Admin works with no issues at all.

Having looked through a number of answered questions here, one did highlight what I believe could be problem, but it was out of context and did not provide much of an explanation.

I am trying to create a frontend entry form for users that corresponds with a foreign key.

models.py

class NewHandoff(models.Model):
    handoff_pk = models.AutoField(primary_key=True)
    handoff_date = models.DateField(auto_now_add=True,verbose_name="Handoff Date")
    shift1_pri = models.ForeignKey(Engineer,on_delete=models.CASCADE,verbose_name="Shift 1 Primary")
    shift1_sec = models.ForeignKey(Engineer,on_delete=models.CASCADE,verbose_name="Shift 1 Secondary")

    def __str__(self):
        return f"{self.handoff_date}"
    
    class Meta:
        verbose_name_plural = 'Handoffs'

# New Handoff Form
class NewHandoffForm(forms.ModelForm):
    class Meta:
        model = NewHandoff
        fields = ['shift1_pri','shift1_sec']

views.py

from django.shortcuts import redirect, render
from django.views import View
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http.response import HttpResponse
from django.contrib import messages
from .models import AttentionForm,  NewHandoffForm

# Handoff View Page
class NewHandoffView(LoginRequiredMixin,View):
    def get(self, request):
        greeting = {}
        greeting['heading'] = "New Handoff"
        greeting['pageview'] = "Handoff"
        return render (request,'handoff/handoff-new.html')

    def post(self, request):
        if request.method == "POST":
            if "add-new-handoff-button" in request.POST:
                create_new_handoff_form = NewHandoffForm(request.POST)
                create_new_handoff_form.save()
                return redirect("/handoff/handoff-create")

handoff-new.html

{% extends 'partials/base.html' %}
{% load static %}
{% load humanize %}
{% load crispy_forms_tags %}

{% block extra_css %}
    <link href="{% static 'libs/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css' %}" rel="stylesheet">
{% endblock %}

{% block contents %}
    <div >
        <div >
            <div >
                <div >
                    <!-- New Form -->
                    <form method="POST">
                        {% csrf_token %}
                        <div >
                            <div >
                                <!-- Field 1 -->
                                <div >
                                    {{ form.shift1_pri|as_crispy_field }}
                                </div>
                                <!-- End of Field 1 -->
                            </div>
                        </div>
                        <div >
                            <button type="submit"  name="add-new-handoff-button">Create New Handoff</button>
                        </div>
                    </form>
                    <!-- End of New Form -->
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block extra_javascript %}
{% endblock %}

Someone mentioned in another post that forms should correlate with the declared form name {{ form.shift1_mod|as_crispy_field }} so it should actually be {{ create_new_handoff_form.shift1_mod|as_crispy_field }} but I have tried changing this and still get the same problem, plus, another model form works fine with just form despite the name of the form being attention_form.

Does anyone have any idea or can point me in the right direction? :)

CodePudding user response:

You are not passing the form through the context in the template. As you are inheriting View, Add the following line in the get() and afterwards in the post() method appropriately:

form = NewHandoffForm()

# and then change return
return render(request,'handoff/handoff-new.html', {'form': form })

Also, you have a space after render in the get function. I hope this is a typo here, but not in your code.

  • Related