Dan
Dan

Reputation: 379

How to write multi line input in 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


Upvotes: 6

Views: 10692

Answers (1)

lifeng.luck
lifeng.luck

Reputation: 601

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 }}

Upvotes: 9

Related Questions