I used this code to read the data from log files and display in browser but its format is very hard to read as there were no line breaks and all text file appear in single paragraph.
data_file = open('/var/log/secure', 'r')
data = data_file.readlines()
variables = RequestContext(request, {
'data1': data,
'var2': 'test'
})
return render_to_response('logfiles.html', variables)
template
{% block content %}
{{ data1 }}
{% endblock %}
Is there any way to keep data same as in original file?
Use
<pre></pre>tags around the output.Also, data_file.readlines() is returning a list… Try data_file.read() instead, and watch out for big files – you can exaust your RAM pretty fast with this code.
At the view you can do simply:
And at the template:
Note that this will not solve the problem with big files halting your server or client, in the real world you may want to limit the display to a few MB and provide a link to download the full file.