时间:2021-07-01 10:21:17 帮助过:18人阅读
urls.py<就几个url,没有什么特殊的>,好吧,我不得不说,那个The Django Book 我只看了下前面的7章,而且还不认真.<http://djangobook.py3k.cn/2.0>
from django.conf.urls import include, url from django.contrib import admin from log import views from django.conf import settings urlpatterns = [ url(r‘^admin/‘, include(admin.site.urls)), url(r‘^$‘,views.login), url(r‘^login/$‘,views.login), url(r‘^loginsuccess/$‘,views.loginsuccess), url(r‘^logout/$‘,views.logout), ]urls.py
log/views.py--------现在目前还没有加上一些判断之类的.比较菜.只是简单的.
from django.shortcuts import render from django.shortcuts import render_to_response from django.http import HttpResponse,Http404 from django import forms from django.contrib import auth from django.http import HttpResponseRedirect import datetime from django.contrib.admin import widgets import subprocess import os.path import fupy #From Post #from django.template import RequestContext class loginForm(forms.Form): username = forms.CharField(max_length=30) password = forms.CharField(widget=forms.PasswordInput) class searchForm(forms.Form): searchform = forms.CharField(label="Please Enter Search Content",min_length=1) day = forms.DateTimeField(label=‘Please Enter Log Date‘,widget=widgets.AdminDateWidget()) def login(request): if request.user.is_authenticated(): return HttpResponseRedirect("/loginsuccess/") error = False if request.method == ‘POST‘: form = loginForm(request.POST) if form.is_valid(): username = request.POST.get(‘username‘,‘‘) password = request.POST.get(‘password‘,‘‘) user = auth.authenticate(username=username,password=password) if user is not None and user.is_active: auth.login(request,user) return HttpResponseRedirect("/loginsuccess/") else: error=True #return HttpResponse("not user") else: form = loginForm() return render_to_response(‘login.html‘,{‘form‘:form,‘error‘:error}) def loginsuccess(request): if request.user.is_authenticated(): if request.method == ‘POST‘: searchform = searchForm(request.POST) if searchform.is_valid(): searchtext = searchform.cleaned_data[‘searchform‘] today = searchform.cleaned_data[‘day‘] today_day = str(today.day).zfill(2) #print today_day #print searchtext if ‘|‘ in searchtext: searchtext_error = True return render_to_response(‘search.html‘,{‘form‘:searchform,‘searchtext_error‘:searchtext_error,‘uname‘:request.user}) logpath = os.path.join(‘/home/var/log/‘,str(today.year),str(today.month),today_day) if not os.path.exists(logpath): file_error = True return render_to_response(‘search.html‘,{‘form‘:searchform,‘file_error‘:file_error,‘uname‘:request.user}) logpath = os.path.join(logpath,‘*‘) cmd = "sudo grep ‘%s‘ %s"%(searchtext,logpath) print cmd showlog = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE) text = showlog.communicate() textshow = text[0].split(‘\n‘) textshow2 = fupy.returnShowLog(textshow) return render_to_response(‘search.html‘,{‘showlog‘:textshow2,‘form‘:searchform,‘uname‘:request.user}) else: searchform = searchForm( initial={‘day‘:datetime.datetime.now().strftime("%Y-%m-%d")} ) return render_to_response(‘search.html‘,{‘form‘:searchform,‘uname‘:request.user}) else: return HttpResponseRedirect("/login/") def logout(request): auth.logout(request) return render_to_response("logout.html")log/views.py
log/templates/base.html-----html模板文件,这里主要是导入bootstrap文件.其它的就没有什么了.
1 {% load staticfiles %} 2 <!DOCTYPE html> 3 <html> 4 <head> 5 <meta charset="utf-8" /> 6 {% block title %}<title>Searchlog</title>{% endblock %} 7 <!-- <script src="{% static ‘bootstrap/js/jquery.ui-1.9.0.min.js‘ %}"></script> --> 8 <script src="{% static ‘bootstrap/js/jquery.min.js‘ %}"></script> 9 <!-- 引入 Bootstrap --> 10 <link href="{% static ‘bootstrap/css/bootstrap.min.css‘ %}" rel="stylesheet"> 11 <script src="{% static ‘bootstrap/js/html5shiv.js‘ %}"></script> 12 <script src="{% static ‘bootstrap/js/respond.min.js‘ %}"></script> 13 <script type="text/javascript" src="{% static ‘bootstrap/js/bootstrap.js‘ %}"></script> 14 <style type="text/css" src="{% static ‘bootstrap/css/bootstrap.css‘ %}"></style> 15 </head> 16 <body> 17 {% block content %} 18 {% endblock %} 19 </body> 20 </html>log/templates/base.html
log/templates/login.html-------登陆窗口页面
1 {% extends "base.html" %} 2 {% block content %} 3 <div class="container"> 4 <div class="row"> 5 <div class="col-md-6 col-md-offset-3"> 6 {% if error %} 7 <div class="alert alert-warning alert-dismissible" role="alert"> 8 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> 9 <strong>提示:</strong>您的账号或者密码有误,请重新进行登陆,谢谢! 10 </div> 11 {% endif %} 12 <div class="jumbotron"> 13 <form class="form-signin" role="form" method="POST" action="">{% csrf_token %} 14 <h2 class="form-signin-heading">Please sign in</h2> 15 <input class="form-control" placeholder="{{ form.username.name }}" required="" autofocus="" type="{{ form.username.name }}" name="{{ form.username.name }}"> 16 <input class="form-control" placeholder="Password" required="" type="password" name="{{ form.password.name }}"> 17 <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button> 18 </form> 19 </div> 20 </div> 21 </div> 22 </div> 23 {% endblock %}log/templates/login.html
log/templates/search.html----查询窗口页面
1 {% extends "base.html" %} 2 {% block content %} 3 <div class="container"> 4 <div class="row"> 5 <div class="col-md-12 col-md-offset-0"> 6 <h3>Hello {{ uname.username }}</h3> 7 <form method="POST" action=""> 8 {% if form.searchform.errors %} 9 <div class="alert alert-success" role="alert"> 10 <strong>警告!</strong> 请输入需要搜索的日志内容!<br> 11 </div> 12 {% endif %} 13 {% if form.day.errors %} 14 <div class="alert alert-success" role="alert"> 15 <strong>警告!</strong> 请输入搜索日志时间! 16 </div> 17 {% endif %} 18 {% if searchtext_error %} 19 <div class="alert alert-warning alert-dismissible" role="alert"> 20 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> 21 <strong>Warning!</strong> 请搜索正确的内容!不要在搜索内容中带有“|”符号,谢谢! 22 </div> 23 <p style="color: red;"></p> 24 {% endif %} 25 {% if file_error %} 26 <div class="alert alert-warning alert-dismissible" role="alert"> 27 <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> 28 <strong>Warning!</strong> 您搜索的日期没有日志,请输入正确的日期,谢谢!格式如:2016-10-10 29 </div> 30 {% endif %} 31 <input class="form-control" placeholder="{{ form.searchform.label }}" type="text" name="{{ form.searchform.name }}" style="width:800px;"> 32 <input class="form-control" type="text" name="{{ form.day.name }}" size="10" value="{{ form.day.value }}" style="width:130px;"> 33 <!-- {{form.as_p}}--> 34 35 <!-- <input type="submit" value="searchlog"> --> 36 <button type="submit" class="btn btn-primary">Search</button> 37 <a href="/logout/">注销登陆</a><br/> 38 <hr> 39 <table class="table table-hover"> 40 <thead> 41 <th>序列</th><th>日志内容</th> 42 </thead> 43 {% for logline in showlog %} 44 <tr> 45 <td>{{ forloop.counter }}</td><td>{{ logline }}</td> 46 </tr> 47 {% endfor %} 48 </table> 49 </form> 50 </div> 51 52 </div> 53 </div> 54 {% endblock %}log/templates/search.html
恩,好吧,大概就是这样一个页面的东西了,没有什么其它的东西了!回想起来还是非常简单的,最麿人的事情是在apache+wsgi的时候,好了!那都是后话了,这样试了下,每天大概有几百M的日志,这样查没有什么问题,响应速度还是不错的。好吧,是好垃圾个,暂时时间与能力有限,先这样,后面再慢慢的进行修改优化下!!!
django+nginx+xshell简易日志查询,接上<关于《rsyslog+mysql+loganalyzer搭建日志服务器<个人笔记>》的反思>
标签:ble code body word shel cte mmu login now()