简体   繁体   English

如何在R xtable的django html渲染中删除\\ n字符?

[英]How do I remove \n characters in django html rendering of an R xtable?

I have an xtable generated in R that I want to insert into a template in Django. 我有一个在R中生成的xtable ,我想将其插入Django的模板中。

The django view is file my_view.py is: django视图的文件my_view.py是:

def my_view(request):
    mtcars = open('mtcars.html').readlines()
    context = {'mtcars':mtcars}
    return render(request, 'my_view.html', context)

The xtable rendered in R appears thusly when using readlines() into python: 因此,当在Python中使用readlines()时, R中呈现的xtable就会出现:

['<!-- html table generated in R 3.1.2 by xtable 1.7-4 package -->\n',
 '<!-- Sun Jan 18 13:32:17 2015 -->\n',
 '<table border=1>\n',
 '<tr> <th>  </th> <th> mpg </th> <th> cyl </th> <th> disp </th> <th> hp </th> <th> drat </th> <th> wt </th> <th> qsec </th> <th> vs </th> <th> am </th> <th> gear </th> <th> carb </th>  </tr>\n',
 '  <tr> <td align="right"> Mazda RX4 </td> <td align="right"> 21.00 </td> <td align="right"> 6.00 </td> <td align="right"> 160.00 </td> <td align="right"> 110.00 </td> <td align="right"> 3.90 </td> <td align="right"> 2.62 </td> <td align="right"> 16.46 </td> <td align="right"> 0.00 </td> <td align="right"> 1.00 </td> <td align="right"> 4.00 </td> <td align="right"> 4.00 </td> </tr>\n',
 '  <tr> <td align="right"> Mazda RX4 Wag </td> <td align="right"> 21.00 </td> <td align="right"> 6.00 </td> <td align="right"> 160.00 </td> <td align="right"> 110.00 </td> <td align="right"> 3.90 </td> <td align="right"> 2.88 </td> <td align="right"> 17.02 </td> <td align="right"> 0.00 </td> <td align="right"> 1.00 </td> <td align="right"> 4.00 </td> <td align="right"> 4.00 </td> </tr>\n',

The template file my_view.html is: 模板文件my_view.html是:

{% extends 'base.html' %}

{% block content %}
    {{mtcars|safe}}
{% endblock %}

The problem is that the safe rendering takes all the lines in the file and shows \\n characters as shown here: 问题在于, safe渲染会占用文件中的所有行并显示\\n字符,如下所示:

在此处输入图片说明

I've tried working around this by replacing the \\n in Python, but I end up with the empty list rows '' . 我尝试通过在Python中替换\\n来解决此问题,但最终出现了空列表行'' If I .read() the file the whole thing gets rendered as a comment (the first row has <!-- ). 如果我将文件.read() ,则整个内容将作为注释呈现(第一行为<!-- )。

Is there a way to control the django safe rendering of the html table to remove the \\n or is there a better way to insert the html table? 有没有一种方法可以控制html表的django safe渲染以删除\\n还是有更好的方法插入html表?

Note: the html table is generated dynamically, otherwise I'd embed it directly (it is rendered appropriately when I embed the text in the template). 注意: html表是动态生成的,否则我会直接将其嵌入(将文本嵌入模板时会适当呈现)。

Just use read() instead of readlines() and you should be good. 只需使用read()而不是readlines()可以了。 The problem is that you are passing along a list instead of a string and you're not unpacking the list. 问题是您传递的是列表而不是字符串,并且没有打开列表的包装。

So the options are a view like this: 所以选项是这样的视图:

def my_view(request):
    mtcars = open('mtcars.html').read()
    context = {'mtcars': mtcars}
    return render(request, 'my_view.html', context)

Or a template like this: 或像这样的模板:

{% extends 'base.html' %}

{% block content %}
{% for row in mtcars %}
    {{ row|safe }}
{% endfor %}
{% endblock %}

You can remove all the whitespaces from the beginning and ending of a string using the .strip() method: 您可以使用.strip()方法从字符串的开头和结尾删除所有空格:

>>> '   \n\r\n\ntest\n   \r\n   \n'.strip()
'test'

In your case this would be: 在您的情况下,这将是:

def my_view(request):
    mtcars = open('mtcars.html').readlines()
    context = {'mtcars': [mtcar.strip() for mtcar in mtcars]}
    return render(request, 'my_view.html', context)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM