如何处理“ | as_crispy_field传递了无效或不存在的字段”错误消息?

时间:2019-07-11 08:47:55

标签: python django django-forms django-templates

我正在制作一个包含几个页面的Web应用程序。在其中一个页面上,我想要一种看起来像这样的表格

https://imgur.com/zo5v8Ni

但是我不断收到一条错误消息:

  

引发CrispyError('| as_crispy_field传递了无效或   不存在的字段”)crispy_forms.exceptions.CrispyError:   | as_crispy_field传递了无效或不存在的字段   [10 / Jul / 2019 23:21:12]“ GET / year2 HTTP / 1.1” 500 148042

这是我的year2.html,它似乎是发生错误的地方:


{% extends "HelloDjangoApp/layout.html" %}
{% load crispy_forms_tags %}

**HelloDjangoApp/year2.html**

{% block content %}
  <form method="post" novalidate>
    {% csrf_token %}
    <div class="row">
      <div class="col-6">
        {{ form.name|as_crispy_field }}
      </div>
      <div class="col-6">
        {{ form.email|as_crispy_field }}
      </div>
    </div>
    {{ form.job_title|as_crispy_field }}
    {{ form.bio|as_crispy_field }}
    <button type="submit" class="btn btn-success">Calculate</button>
  </form>
{% endblock %}

对于上下文,这是我的views.py


from django.shortcuts import render
from django.http import HttpResponse
from datetime import datetime
from django.shortcuts import render   # Added for this step
from django.views.generic import CreateView
from .models import Person

class PersonCreateView(CreateView):
    model = Person
    fields = ('name', 'email', 'job_title', 'bio')


def index(request):
    now = datetime.now()

    return render(
        request,
        "HelloDjangoApp/index.html",  # Relative path from the 'templates' folder to the template file
        # "index.html", # Use this code for VS 2017 15.7 and earlier
        {
            'title' : "Exam Results Calculator",
            'message' : "Hello!",
            'content' : " on " + now.strftime("%A, %d %B, %Y at %X"),
        }
    )

def about(request):
    return render(
        request,
        "HelloDjangoApp/about.html",
        {
            'title' : "Simply enter your scores to find out your average",
            'content' : "Example app page for Django."
        }
    )

def year1(request):
    return render(
        request,
        "HelloDjangoApp/year1.html",
        {
            'title' : "1st Year Average",
            'content' : "First Year Stuff"

        }

    )

def year2(request):
    return render(
        request,
        "HelloDjangoApp/year2.html",
        {
            'title' : "2nd Year Average",
            'content' : "Second Year Stuff"

        }

    )


def year3(request):
    return render(
        request,
        "HelloDjangoApp/year3.html",
        {
            'title' : "3rd Year Average",
            'content' : "3rd Year Stuff"

        }

    )

def year4(request):
    return render(
        request,
        "HelloDjangoApp/year4.html",
        {
            'title' : "4th Year Average",
            'content' : "4th Year Stuff"

        }

    )
# Create your views here.


和我的模型。py

from django.db import models

# Create your models here.
class Person(models.Model):
    name = models.CharField(max_length=130)
    email = models.EmailField(blank=True)
    job_title = models.CharField(max_length=30, blank=True)
    bio = models.TextField(blank=True)

2 个答案:

答案 0 :(得分:0)

请重新检查您的CBV-PersonCreateView。 您在year2.html中访问的表单字段不属于正在处理它的视图的表单实例。 1)将year2.html模板重命名为Person_form.html 2)从基于year2函数的视图中删除“ HelloDjangoApp / year2.html”。

希望这会有所帮助,谢谢。

答案 1 :(得分:0)

所有功能中的returnrender必须{'form': form}

执行此操作:

def year2(request):
    form = Person()
    return render(
        request,
        "HelloDjangoApp/year2.html",
        {
            'form': form
        }

    )