Home>

class-based viewI'm implementing a blog at.
CreateViewI once confirmed that I could fill out the form using
afterwards,create.htmlTobootstrapI'm stumbling with an error when I try to reflect.
bootstrapWe would appreciate it if you could give us some advice on how to introduce.
In addition, it should be notedbootstrapIsdjango-bootstrap4I am using.

The error message that is occurring
Parameter "form" should contain a valid Django Form.
Request Method: GET
Request URL: http://127.0.0.1:8000/create/
Django Version: 3.1.2
Exception Type: BootstrapError
Exception Value:
Parameter "form" should contain a valid Django Form.
Exception Location: /opt/anaconda3/envs/django-env/lib/python3.9/site-packages/bootstrap4/renderers.py, line 145, in __init__
Python Executable:/opt/anaconda3/envs/django-env/bin/python
Python Version: 3.9.0
Corresponding source code

blogproject/templates/create.html

{% extends "base.html"%}
{% load bootstrap4%}
{% block title%} Blog {% endblock%}
{% block content%}
<form action = "" method = "POST">
  {% csrf_token%}
  {% bootstrap_form form.as_p%}
<input type = "submit" value = "create">
</form>
{% endblock%}

Of the above code

{% bootstrap_form form.as_p%}


To

{{form.as_p}}


As long as it is set to, it works fine.
By the way, although it is different from the above,blogproject/templates/list.htmlInbootstrapIs reflected without any problem.

Other code is written as follows.
blogproject/templates/base.html

<! DOCTYPE html>
{% load bootstrap4%}
{% bootstrap_css%}
{% bootstrap_javascript jquery ='slim'%}
<html>
<head>
  <meta charset = "utf-8">
  <meta http-equiv = "X-UA-Compatible" content = "IE = edge">
  <meta name = "viewport" content = "width = device-width, initial-scale = 1, shrink-to-fit = no">
  <title>{% block title%} {% endblock%}</title>
    {# --- css --- #}
    {% block extra_css%} {% endblock%}
</head>
<body>
  <main>
      {% block content%} {% endblock%}
  </main>
    {# --- js --- #}
    {% block extra_js%} {% endblock%}
</body>
</html>

blogproject/blogpost/views.py

from django.shortcuts import render
from django.urls import reverse_lazy
from django.views.generic import ListView, DetailView, CreateView
from .models import BlogModel

class BlogList (ListView):
    "" "List display" ""
    template_name ='list.html'
    model = BlogModel
class BlogDetail (DetailView):
    """ Detail View """
    template_name ='detail.html'
    model = BlogModel
class BlogCreate (CreateView):
    "" "Article creation" ""
    template_name ='create.html'
    model = BlogModel
    fields = ('title','content')
    success_url = reverse_lazy ('list')

blogproject/blogpost/urls.py

from django.urls import path
from .views import BlogList, BlogDetail, BlogCreate
urlpatterns = [
    path ('list /', BlogList.as_view (), name ='list'),
    path ('detail/<int: pk>/', BlogDetail.as_view (), name ='detail'),
    path ('create /', BlogCreate.as_view (), name ='create'),
]
  • Answer # 1

    django-bootstrap4As you can see in the READMEbootstrap_formIsn't it the Django Form object that should be passed to?

    {% extends "base.html"%}
    {% load bootstrap4%}
    {% block title%} Blog {% endblock%}
    {% block content%}
    <form action = "" method = "POST">
      {% csrf_token%}
    -{% bootstrap_form form.as_p%}
    + {% bootstrap_form form%}
    <input type = "submit" value = "create">
    </form>
    {% endblock%}