4

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?

1
  • A possible unixy solution, if you simply wanted to read the latest in the logs for yourself in terminal, would be to run tail -f /var/log/secure
    – Jordan
    May 27, 2011 at 7:53

3 Answers 3

3

Use <pre></pre> tags around the output.

{% block content %}
     <pre>{{ data1 }}</pre>
{% block content %}

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:

data_file = open('/var/log/secure', 'r') 

variables = RequestContext(request, {
        'data_file': data_file,
        'var2': 'test'
})
return render_to_response('logfiles.html', variables)

And at the template:

{% block content %}
    <pre>{{ data_file.read }}</pre> 
{% endblock %}

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.

4
  • I tried that as well then it shows everything (500lines) in one line only
    – Mirage
    May 27, 2011 at 6:35
  • @bidu: I forgot the <pre> tags; the linebreaks filter from DrTyrsa's answer also work, but waist some bandwidth for the <br /> tags and can worse the problem with big files. Also <pre> gives you a nice monospaced font. May 27, 2011 at 7:06
  • @Paulo that looks very good . i liked that. can i display only few lines like 100. can i read file in reverse order like most recent first
    – Mirage
    May 27, 2011 at 7:23
  • Python cookbook has some recipes for reading a file backwards; you can start from code.activestate.com/recipes/276149 or code.activestate.com/recipes/120686 May 27, 2011 at 7:35
1

Use linebreaks filter

4
  • i tried this {{ data1 | linebreaks }} and i get error could not parse linebreaks
    – Mirage
    May 27, 2011 at 6:52
  • @bidu: because data1 (AKA data_file.readlines()) is not a string, its a list of strings. Use data_file.read() instead. May 27, 2011 at 6:58
  • @Dr sorry budy but pauls solution was monospaced and easy to read but thanks for the info i will use it in future
    – Mirage
    May 27, 2011 at 7:24
  • @bidu You can style any tag to use monospaced font with CSS. And using linebreaks is more semantic, then just pre. But it's your choice, ok. :-)
    – DrTyrsa
    May 27, 2011 at 7:34
0

Try {{ data|linebreaks }} or {{ data | linebreaksbr }} this will help retaining the line breaks in your data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.