Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write multi line input in Django

Tags:

django

I am making a very basic BBS system (like blog) in Django.

I've made a form where user can type in their content which I call 'body' and I declare it as folowing in the forms.py:

body = forms.CharField(widget= forms.Textarea, label="body",required=True)

and in the models.py,

body = models.TextField()

I somehow can't seem to be able to write multi line texts...

To sum it up,

I can write multiple lines into my form like:

Hello this is test

This is sample test2

333333

But When I submit it, I only see

Hello this is test This is sample test2 333333


like image 625
Dan Avatar asked Jan 11 '23 02:01

Dan


1 Answers

The error happened when display it.

Textarea uses cr\lf to break lines. That is how it would be saved into db. But html will ignore cr\lf you need to replace cr\lf with <br> or <p>

A template tag linebreaks can help you, when you display it.

 {{ value|linebreaks }}
like image 121
lifeng.luck Avatar answered Jan 16 '23 21:01

lifeng.luck