【问题标题】:BootstrapError Form引导错误表单
【发布时间】:2021-06-30 19:56:40
【问题描述】:

我的模板中出现此错误:

异常值:参数“form”应包含有效的 Django 表单。

我的forms.py:

from django import forms
from .models import CustomerProfile

class CustomerForm(forms.ModelForm):
    class Meta:
        model = CustomerProfile
        fields = ('name', 'email', 'phone', 'business')
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'email': forms.EmailInput(attrs={'class': 'form-control'}),
            'phone': forms.TextInput(attrs={'class': 'form-control'}),
            'business': forms.Select(attrs={'class': 'form-control'}),
        }

在我的views.py中我这样做了:

from django.http.response import HttpResponseRedirect
from django.urls import reverse
from django.shortcuts import render

from .forms import CustomerForm

def index(request):
    return render(request, 'landingpage/index.html')

def about(request):
    return render(request, 'landingpage/about.html')

def new_contact(request):

    if request.method == 'POST':
        form = CustomerForm(request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect(reverse('landingpage:thanks'))
    else:
        form = CustomerForm()

    return render(request, 'landingpage/index.html', {'form': form})


def thanks(request):
    return render(request, 'landingpage/thanks.html')

我的 index.html 表单部分:

<!-- Contact Section-->
        <section class="page-section" id="contact">
            <div class="container">
                <!-- Contact Section Heading-->
                <h2 class="page-section-heading text-center text-uppercase text-secondary mb-0">Contact Me</h2>
                <!-- Icon Divider-->
                <div class="divider-custom">
                    <div class="divider-custom-line"></div>
                    <div class="divider-custom-icon"><i class="fas fa-star"></i></div>
                    <div class="divider-custom-line"></div>
                </div>
                <!-- Contact Section Form-->
                <div class="form-floating mb-3">
                    <div class="row justify-content-center">
                        <div class="col-lg-8 col-xl-7">
                            <form action="{% url 'landingpage:index' %}" method="post" class="form">
                                {% csrf_token %}
                                {% bootstrap_form form %}
                                {% buttons %}
                                <button class="btn btn-primary btn-xl enabled" id="submitButton" type="submit">Send</button>
                                {% endbuttons %}

                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </section>

有什么想法吗? 我尝试了很多更改,但都没有解决问题。 有人已经陷入这个困境了吗?

【问题讨论】:

  • 请分享完整回溯。
  • forms.py 文件中的变量fields 必须是一个列表,所以尝试用括号改变括号,这样你就有了:fields = ['name', 'email', 'phone', 'business']
  • 也可以尝试在模板{% bootstrap_form form %}中去掉%,当你在你的模板中调用一个表单时,你应该写{{ form }}
  • 不错。我不再收到错误,但是当我在本地主机中运行模板时表单没有显示

标签: python django bootstrap-5


【解决方案1】:

在您的模板更改中

{% bootstrap_form form %}

{{ form }}

您应该记录自己如何在模板中使用变量以及如何呈现表单:Django Form Documentation

【讨论】:

  • 不错。我不再收到错误消息,但是当我在本地主机中运行模板时,表单根本不显示。
  • 我的模板源代码基于这个主题:startbootstrap.com/theme/freelancer 我正在将默认表单更改为我的应用程序中的表单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 2017-08-31
  • 1970-01-01
  • 2013-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多