Home>

With Djangoclass-based viewI am making a blog application using.
For decorationdjango-bootstrap4I am using, but I am thinking of making it a specification that allows you to enter multiple lines in the input form.

The problem i am having

textareaWith elementscreate.htmlI created a file, but the text is output in one line on the destination blog itself.

Related source code

blogproject/templates/create.html

{% extends "base.html"%}
{% load bootstrap4%}
{% block title%} Newly created {% endblock%}
{% block content%}
<form action = "" method = "POST">
  {% csrf_token%}
    
      <label>Title</label>
        
          <input type = "text" name ='title' placeholder = "title">
        
    
    
      <label>Contents</label>
        
          <textarea name ='content' rows = "5" placeholder = "content"></textarea>
        
    
    
        
          <button type = "submit">Create</button>
        
    
</form>
{% endblock%}


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, DeleteView
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/models.py

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
  • Answer # 1

    I created a create.html file using the textarea element, but the text is output in one line on the destination blog itself.

    Since the line break via the textarea is a line break code, it is not a line break in the browser when viewed as HTML.

    I don't know if it's correct because there is no template for the person displaying the blog,
    Django has a filter that converts line breaks to
    tags for HTML, so
    If you are not using it, please try it.

    Example of use

    {{article.content | linebreaks}}

    ※document