Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

BootstrapError: Parameter "form" should contain a valid Django Form

I keep getting the above error (in the title) for my DeleteView. Strangely my UpdateView, UpdateObject form and update_object template are identical to my DeleteView, DeleteObject form and delete_object template respectively. I'm not sure why bootstrap is giving me an error for this form and not my update form? Here are the files:

forms.py:

from django import forms
from . import models
from django.forms import ModelForm

class CreateBouquetForm(ModelForm):

   class Meta:
       model = models.Bouquet
       exclude = ('price',)

   def __init__(self,*args,**kwargs):
       super().__init__(*args,**kwargs)


class UpdateBouquetForm(ModelForm):

    class Meta:
       model = models.Bouquet
       exclude = ('price',)


class DeleteBouquetForm(ModelForm):

   class Meta:
       model = models.Bouquet
       exclude = ('price',)

` views.py:

class UpdateBouquet(UpdateView):
    model = models.Bouquet
    form_class = forms.UpdateBouquetForm
    template_name = 'products/update_bouquet.html'
    success_url = reverse_lazy('products:shop')

    def form_valid(self,form):
        self.object = form.save(commit=False)
        result = 0
        for flower in self.object.flower.all():
            result += flower.price
        self.object.price = result
        self.object.save()
        return super().form_valid(form)


class DeleteBouquet(DeleteView):
    model = models.Bouquet
    form_class = forms.DeleteBouquetForm
    template_name = 'products/delete_bouquet.html'
    success_url = reverse_lazy('products:shop')

products/update_bouquet.html:

{% extends 'site_base.html' %}
{% load bootstrap3 %}

{% block content_block %}

<form method="post">
  {% csrf_token %}
  {% bootstrap_form form %}
  <input type="submit" class="btn btn-outline-success" value="Save bouquet and continue shopping">
</form>

<h6><a href="{% url 'products:detail_bouquet' pk=bouquet.pk %}">Back to bouquet</a></h6>

{% endblock %}

products/delete_bouquet.html:

{% extends 'site_base.html' %}
{% load bootstrap3 %}

{% block content_block %}

<h5>Are you sure you want to delete your bouquet?</h5>

<form method="POST">
  {% csrf_token %}
  {% bootstrap_form form %}
  <input type="submit" class="btn btn-outline-success" value="Delete">
</form>

<h6><a class="btn btn-outline-danger" href="{% url 'products:detail_bouquet' pk=bouquet.pk %}">Cancel</a></h6>


{% endblock %}

urls.py:

    url(r'^bouquet/(?P<pk>d+)/update/$',views.UpdateBouquet.as_view(),name="update_bouquet"),
    url(r'^bouquet/(?P<pk>d+)/delete/$',views.DeleteBouquet.as_view(),name="delete_bouquet"),

Here's what the error page says:

Error during template rendering
In template C:UsersjoannDesktopFlowers_Storefrontstorefrontemplatessite_base.html, error at line 8

Parameter "form" should contain a valid Django Form.
1   <!DOCTYPE html>
2   {% load static %}
3   {% load bootstrap3 %}
4   <html lang="en" dir="ltr">
5     <head>
6       <meta charset="utf-8">
7       <title></title>
8       <link href="https://cdn.jsdelivr.net/**npm/[email protected]/dist/css/boo**tstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
9       <link rel="stylesheet" href="{% static "/css/css_master.css" %}">
10      <link rel="preconnect" href="https://fonts.gstatic.com">
11 class="container-fluid"> 

...etc....

Thanks in advance if you can help!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

DeleteView does not do any form handling and so does not add a "form" key to the context. Remove form_class from your view and remove the bootstrap_form tag from your template

class DeleteBouquet(DeleteView):
    model = models.Bouquet
    template_name = 'products/delete_bouquet.html'
    success_url = reverse_lazy('products:shop')
{% extends 'site_base.html' %}

{% block content_block %}

<h5>Are you sure you want to delete your bouquet?</h5>

<form method="POST">
  {% csrf_token %}
  <input type="submit" class="btn btn-outline-success" value="Delete">
</form>

<h6><a class="btn btn-outline-danger" href="{% url 'products:detail_bouquet' pk=bouquet.pk %}">Cancel</a></h6>

{% endblock %}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...