从表格djang输入时打印

2024-04-19 02:58:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个模型:

 class somemodel(models.Model):
      message=models.TextField()

      def __str__(self):
         return self.message

现在我已经为这个模型制作了一个表单,并从用户那里获取了输入。假设用户输入如下:

  Hi i am a djago developer and i am developing a website . 

  Bellow are my contact details:

   Address: xyz
   mobile:123456
   pin:1234

但当我打印出来的时候,这就像吼叫一样。你知道吗

   Hi i am a djago developer and i am developing a website . 
   Bellow  are  my contact details: Address: xyz mobile:123456 pin:1234

有没有可能打印出用户输入的内容,因为它已经键入。请建议。你知道吗


Tags: and用户模型selfdevelopermessagemodelsmy
2条回答

可以使用文本编辑器将文本字段保存为html。 django-ckeditor似乎是个不错的选择。 https://github.com/django-ckeditor/django-ckeditor

问题与文本的输入或存储方式无关。你知道吗

HTML总是忽略空白,包括换行符。如果要显示带换行符的文本,则需要插入换行符的HTML标记。你知道吗

幸运的是,有一个内置的Django模板标记可以将换行符转换为HTML标记;毫不奇怪,它被称为linebreaks。因此,如果将文本作为message传递给模板:

{{ message|linebreaks }}

相关问题 更多 >