I'm making a blogging app with class-based view in Django.
I'm using django-bootstrap4 for decoration, but I'm stumbling upon implementing a specification to enter multiple lines in an input form.

I have been able to display the input form, but
Even if I press the submit button, only the title and date are reflected in the blog itself, not the text.
(As described later, it can be implemented if the specification is to enter the text in one line instead of multiple lines)

I would appreciate any advice.

Even if I press the submit button, only the title and date are reflected in the blog itself, not the text.

Corresponding source code


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

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
    form_class = BlogForm
    success_url = reverse_lazy ('list')


from django.db import models

class BlogModel (models.Model):title = models.CharField (max_length = 100)
    content = models.TextField ()
    postdate = models.DateField (auto_now_add = True)
    def __str __ (self):
        return self.title


from django import forms
from .models import BlogModel
class BlogForm (forms.ModelForm):
    content = forms.CharField (widget = forms.Textarea)
    class Meta:
        model = BlogModel
        fields = ('title',)


from django.urls import path
from .views import BlogList, BlogDetail, BlogCreate, BlogDelete, BlogUpdate
urlpatterns = [
    path ('list /', BlogList.as_view (), name ='list'),
    path ('detail/<int: pk>/', BlogDetail.as_view (), name ='detail'),
    path ('create /', BlogCreate.as_view (), name ='create'),
    path ('delete/<int: pk>', BlogDelete.as_view (), name ='delete'),
    path ('update/<int: pk>', BlogUpdate.as_view (), name ='update'),


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


<! 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%}
    {% block header%} {% endblock header%}
</head><body><main>    {% block content%} {% endblock content%}
  </main>  {# --- js --- #}
    {% block extra_js%} {% endblock%}
What I tried

Originally blogproject/blogpost /forms.pyHave not createdmodels.pyFromviews.pyI used to communicate directly with
In that case, the input of the text was also reflected in the blog itself without any problem.

For specifications that allow multiple lines to be entered,textareaI think it is necessary to setforms.pyI tried to realize it with
forms.pyFromviews.pyI think that the data is not connected well.

views.pyI tried removing model = BlogModel from my BlogCreate class, but the results didn't change.

On the other handforms.pyOf the Meta class of the BlogForm classfieldsIf i add content to the element in addition to title, the text will be reflected in the blog itself,
(Substantiallyforms.pyEven if you try to enter in multiple lines (because the function of) will be lost, it will be reflected in one line.

Supplementary information (FW/tool version, etc.)

Django Version: 3.1.2
Python Version: 3.9.0

  • Answer # 1

    Self-solved by doing the following processing (multi-line input is now reflected in the blog body)
    (I referred to the advice given by attakei here)

    list.htmlChanged the relevant part of

    Change before


    After change

       {{item.content | linebreaks}}

    views.pyofBlogCreateRemove model = BlogModel from class

    forms.pyofBlogFormIn classMetaclassfieldsIn addition to title, content is also added to the element