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

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

我正在尝试为现有模型创建前端数据输入页面。但是,当单击 link 时,出现错误:

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

明确一点,从 Django Admin 添加数据完全没有问题。

在这里浏览了一些已回答的问题,其中一个确实强调了我认为可能存在的问题,但它断章取义,没有提供太多解释。

我正在尝试为用户创建一个与外键相对应的前端输入表单。

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")

切换-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 class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-body">
                    <!-- New Form -->
                    <form method="POST">
                        {% csrf_token %}
                        <div class="row">
                            <div class="row-fluid pb-1">
                                <!-- Field 1 -->
                                <div class="mb-3">
                                    {{ form.shift1_pri|as_crispy_field }}
                                </div>
                                <!-- End of Field 1 -->
                            </div>
                        </div>
                        <div class="d-flex flex-wrap gap-2">
                            <button type="submit" class="btn btn-primary waves-effect waves-light" name="add-new-handoff-button">Create New Handoff</button>
                        </div>
                    </form>
                    <!-- End of New Form -->
                </div>
            </div>
        </div>
    </div>
{% endblock %}

{% block extra_javascript %}
{% endblock %}

有人在另一个 post 中提到表单应该与声明的表单名称相关联 {{ form.shift1_mod|as_crispy_field }} 所以它实际上应该是 {{ create_new_handoff_form.shift1_mod|as_crispy_field }} 但我已经尝试更改它但仍然遇到同样的问题, 另外,尽管表单的名称是 attention_form.

,但另一个模型表单可以正常工作

有没有人知道或可以指出正确的方向? :)

您没有通过模板中的上下文传递表单。当您继承 View 时,在 get() 中添加以下行,然后在 post() 方法中适当添加:

form = NewHandoffForm()

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

此外,您在 get 函数中渲染后有一个 space。我希望这是一个错字,但不是在您的代码中。