我想创建一个可以拖尾文件的视图,比如Unix中的tail -f
。
我知道有些python模块已经这样做,比如tail
。例如,我可以在我的cmd
控制台中运行此示例:
import tail
def print_line(txt):
''' Prints received text '''
print(txt)
t = tail.Tail('C:\\temp.txt')
t.register_callback(print_line)
t.follow(s=1)
但是如何在python中的views.py
中使用它?并保持清爽,而不是整个页面,但我的textarea只? ?
def logs(request):
if request.method == 'POST':
#.. user inform which logs would like to tail
# 'out' is the output from the tail function
return render(request, 'logs.html', {'out': out})
return render(request, 'logs.html')
我的.html
文件:
<form action="/logs/" method="post">{% csrf_token %}
<div class="panel-body">
<div class="row">
<div class="col-md-2 form-group">
<select class="form-control m-bot15" name="command" required>
<option value="">Choose a system log</option>
<option value="log1">LOG 1</option>
</select>
</div>
<input type="checkbox" value="tail" name="tail" id="tail">Tail?
<button type="submit" class="btn btn-primary"><i class="fa fa-arrow-circle-o-right"></i> RUN!</button>
<div class="col-md-12 form-group">
<textarea class="form-control" rows="30">{{ out }}</textarea>
</div>
</div>
</div>
</form>