'Paragraphs get lost when posting a form
Hey, I'm creating a forum using django. When a user posts a thread the written text (in the input field {{obj.content}}) by the user should be displayed exactly as he typed it in. The text is displayed through {{form.content}}.
That means the paragraphs which were made by the user should be maintained. But in the moment they get lost and the text is displayed without paragraphs.
create_thread.py (on this page a thread can be created):
{% extends "forum/index.html" %}
{% load static %}
{% block title %} {{post.title}} {% endblock %}
{% block content %}
{% for obj in objects %}
<div class="container-thread">
<div class="username">{{obj.username}}</div>
<div class="topic">{{obj.topic}}</div>
<div class="content">{{obj.content}}</div>
</div>
{% endfor %}
{% endblock %}
thread.html (on this page the thread is displayed):
<div class="container-create-thread">
<h2 class="heading-create-thread">Create a thread</h2>
<form method="POST" action="">
{% csrf_token %}
<label class="label-topic">Topic</label>
{{form.topic}}
<label class="label-title">Title</label>
{{form.title}}
<label class="label-content">Content</label>
{{form.content}}
<button class="btn submit" id="submit-create-thread" type="submit" value="submit">Submit</button>
</form>
</div>
Solution 1:[1]
The paragraphs are probably not lost. What is happening is that line breaks in text inside HTML are not shown as line breaks when rendered on the page.
You need to replace line breaks with <br>
. You should also replace <
with <
and >
with >
. Otherwise your page will be vulnerable to script injection.
You can test it now: enter <script>window.alert('hello')</script>
into the form and see what happens when you submit the form.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | md2perpe |