css - How to Preserve White Space and line breaks in Django?
问题描述
I am making a project where I want to display a user's code. I am using Django's forms. But when I post that form, the form doesn't preserve the white space and the line breaks.
Can you please help me?
# this is my form
class VigenereCipherDiscussForm(forms.ModelForm):
class Meta:
model = VigenereCipherDiscuss
fields = ['text', 'share']
widgets = {
"text": forms.Textarea(attrs={"class":"form-control", "rows":4}),
"share": forms.Textarea(attrs={"class":"form-control", "rows":5, "placeholder": "Put in your cipher text or code in here to share with others"})
}
# (stackoverflow preserves the line breaks, the "class Meta" is indented nicely)
If i have this code:
x = 2
if x == 2:
return "2"
else:
return None
# I want this code to be outputted in the form the way it is right now!
But django gives me
x=2 if x==2: return "2" else return None
解决方案
Use linebreak
change your code to:
x=2\nif x==2:\nreturn "2"\nelse:\nreturn None
and in template:
{{ value|linebreaks }}
推荐阅读
- python-3.x - 评估一张图像的 CNN 模型
- php - 从 laravel artisan 的命令行下载文件
- r - 正则表达式在 R 中分割位置?
- python - 如何在 PySpark 中过滤固定、常规时间段的记录?
- python - django 类别 -id 值
- java - Java 同步取决于方法参数
- javascript - 如果字符串不是对象,我如何使用内置方法?
- spring-boot - Thymeleaf 和 th:如果在 null 时不跳过文本
- java - 如何将 JavaFX 滑块绑定到显示列表中值的标签?
- flutter - 如何从 Flutter 中的列表视图子项中单独获取数据?