English 中文(简体)
如何在Python Django模板中打印换行符
原标题:How to print line breaks in Python Django template
{ quotes : u Live before you die.

"Dream as if you ll live forever, live as if you ll die today"

"Love one person, take care of them until you die. You know, raise kids. Have a good life. Be a good friend. Try to be completely who you are, figure out what you personally love and go after it with everything you ve got no matter how much it takes." -Angelina Jolie. }

Notice my dictionary has line breaks in them:

如何显示带有那些换行的模板?

{{quotes|withlinebreaks }}

最佳回答

使用linebreaks过滤器。

例如:

{{ value|linebreaks }}

If value is Joel is a slug, the output will be <p>Joel<br />is a slug</p>.

问题回答

You can also use the linebreaksbr filter to simply convert all newlines to <br> without additional <p>.

例子:

{{ value|linebreaksbr }}

If value is Joel is a slug, the output will be Joel<br>is a slug.

Ignacio s answer (linebreaks filter) 的区别在于,linebreaks 尝试猜测文本中的段落并将每个段落包装在 <p> 中,而 linebreaksbr 简单地将换行符替换为 <br>

这里有一个演示:

>>> from django.template.defaultfilters import linebreaks
>>> from django.template.defaultfilters import linebreaksbr
>>> text =  One
break

Two breaks


Three breaks 
>>> linebreaks(text)
 <p>One<br />break</p>

<p>Two breaks</p>

<p>Three breaks</p> 
>>> linebreaksbr(text)
 One<br />break<br /><br />Two breaks<br /><br /><br />Three breaks 

所有这些答案都没有明确提到,在显示模板中应该使用{{ value|linebreaksbr }},即在获取请求而不是提交请求中使用。

那么,如果您有一个模板来显示发帖的详细信息,那就会是:

<h4>Below are the post details</h4>

{{ post.details|linebreaksbr }}

而不是在邮寄形式中

<form method="post">
    {% csrf_token %}
    {{ form|linebreaksbr }}
    <button>Save post</button>
</form>

Had this same issue and after figuring it our decided that someone outthere might find it handy. Happy coding!

出于某种原因,一概不能改变这一价值

val = """
Brand AER 
Detail Material Kuningan berlapis emas chrome dan powder coating 
Tipe Produk Keran Shower 
Warna Emas 
Material Kuningan 
Ukuran Kemasan 12cm x 19cm x 13cm 
Ukuran Barang 12cm x 19cm x 13cm 
Berat 2kg 
Manual Instruksi instalasi manual dapat didownload di link berikut:https://aer.co.id/products/aer-kran-shower-panas-dingin-luxury-series-sah-sr1/?attachment_id=10954&amp;download_file=5e267c93cdc76&amp;_wpnonce=28db09bc61
"""

which has in it with linebreaks or linebreaksbr filter i ve tried this

{ val|linebreaks }}
{ val|linebreaksbr }}

the val stays the same then i tried @AndrewFox answer and did this and it worked for me

Change to <br> and autoescape off

{% autoescape off %}
   {{ val }}
{% endautoescape %}




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签