首页 > 代码库 > django部分代码

django部分代码

ngingx配置文件更新代码,和展示

Python代码

# -*- coding: utf-8 -*-
from django.shortcuts import render
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django.contrib import auth
from django.contrib.auth.decorators import login_required
import difflib
import os, sys
import requests 
import re
import urllib, urllib2
from django.core.paginator import Paginator
import commands
import MySQLdb
import time, datetime
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.template import RequestContext
from webip.models import AccessDizhi
from nginxet.models import NginxET, NginxEThistory
from django.shortcuts import get_object_or_404
from nginxet.form import HostsListForm
@csrf_exempt
def nginxfile(request):
    if request.method == ‘POST‘:
        date1 = datetime.datetime.now()
        date2 = date1 + datetime.timedelta(hours=14)
        date = date2.strftime(‘%Y-%m-%d %H:%M:%S‘)
        all_server1 = NginxET.objects.all().values_list(‘dirname‘,‘filename‘)
        c = []
        for i in all_server1:
            c.append(i)
        for root,dirs,files in os.walk(‘/data/nginxet/‘):
            dir_name = root.split(‘/‘)[3]
            if dir_name:
               for filed in files:
                   if filed:
                      filed3 = (u‘%s‘ %dir_name, u‘%s‘ %filed)
                      abc = c.count(filed3)
                      if abc > 0:
                          pass
                      else:
                          filedd = root+‘/‘+filed
                          filed1 = open(filedd, ‘r‘)
                          messages = filed1.read()
                          NginxET.objects.create(dirname=dir_name,filename=filed,message=messages,onlinetime=date,updatemessage=1,updatetime=date)
                          filed1.close()
                   else:
                      pass  
        all_server = NginxET.objects.all()
        paginator = Paginator(all_server,20)
        try:
            page = int(request.GET.get(‘page‘,‘1‘))
        except ValueError:
            page = 1
        try:
            all_server = paginator.page(page)
        except :
            all_server = paginator.page(paginator.num_pages)
        return render_to_response(‘nginxet.html‘,
                      {‘all_host_list‘: all_server, ‘page‘: page, ‘paginator‘:paginator},context_instance=RequestContext(request))
    else:
        all_server = NginxET.objects.all()
        paginator = Paginator(all_server,20)
    
        try:
            page = int(request.GET.get(‘page‘,‘1‘))
        except ValueError:
            page = 1
    
        try:
            all_server = paginator.page(page)
        except :
            all_server = paginator.page(paginator.num_pages)
        return render_to_response(‘nginxet.html‘,
                          {‘all_host_list‘: all_server, ‘page‘: page, ‘paginator‘:paginator},context_instance=RequestContext(request))
models代码
# -*- coding: utf-8 -*-
from django.db import models
class NginxET(models.Model):
    dirname = models.CharField(max_length=50, verbose_name=u‘目标地址‘)
    filename = models.CharField(max_length=100, verbose_name=u‘文件名‘)
    message = models.TextField(verbose_name=u‘nginx内容‘)
    updatemessage = models.TextField(verbose_name=u‘更新内容‘)
    onlinetime = models.DateTimeField(max_length=100, blank=True, verbose_name=u‘上线时间‘)
    updatetime = models.DateTimeField(max_length=100, blank=True, verbose_name=u‘更新时间‘)
    def __unicode__(self):
        return u‘%s - %s - %s - %s‘ %(self.dirname, self.filename, self.message, self.updatemessage, self.updatetime)
    class Meta:
        verbose_name = u‘目标地址‘
        verbose_name = u‘文件名‘
form代码
# -*- coding: utf-8 -*-
from django import forms
from nginxet.models import NginxET
class HostsListForm(forms.ModelForm):
#class HostsListForm(forms.Form):
    class Meta:
        model = NginxET
        fields = "__all__"
        widgets = {
          ‘dirname‘: forms.TextInput(attrs={‘class‘: ‘form-control‘}),
          ‘filename‘: forms.TextInput(attrs={‘class‘: ‘form-control‘}),
#          ‘message‘: forms.CharField(widget=forms.Textarea),
          ‘message‘: forms.TextInput(),
          ‘updatemessage‘: forms.TextInput(attrs={‘class‘: ‘form-control‘}),
          ‘onlinetime‘: forms.TextInput(attrs={‘class‘: ‘form-control‘}),
          ‘updatetime‘: forms.TextInput(attrs={‘class‘: ‘form-control‘}),
        }

html代码

{% extends "index.html" %}
{% block title %}OMS{% endblock %}
{% block css %}
<!-- DataTables-->
<link rel="stylesheet" href="http://www.mamicode.com/static/plugins/dataTables/css/dataTables.css">
{% endblock %}
{% block content %}
      <form action="http://:8999/nginxet/" method="POST">
      <div class="row">
            <div class="col-md-12">
                 <div class="panel panel-default">
                      <div class="panel-body">
                           {% if all_host_list %}
                           <table id="example" class="table table-striped table-bordered" cellspacing="0" width="100%">
                               <thead>
                                   <tr>
                                      <th>ID</th>
                                      <th>IP地址</th>
                                      <th>文件名</th>
                                      <th>上线时间</th>
                                      <th>更新时间</th>
                                   </tr>
                               </thead>

                               <tbody>
                               {% for all_host in all_host_list %}
                                   <tr>
                                      <td>{{ forloop.counter }}</td>
                                      <td>{{ all_host.dirname }}</td>
                                      <td>{{ all_host.filename }}</td>
                                      <td>{{ all_host.onlinetime }}</td>
                                      <td>{{ all_host.updatetime }}</td>
                                      <td><a href="http://www.mamicode.com/{% url ‘message‘ id=all_host.id %}" class="btn btn-info btn-sm">内容展示</a>
                                          <a href="http://:8999/nginx_update/?update=1" style="color:white" class="btn btn-danger btn-sm" role="button">历史</a>
                                          <a href="http://:8999/nginx_diff/?id={{ all_host.id }}" class="btn btn-info btn-sm">最近一次更改比较</a>
                                          <a href="http://:8999/nginx/?id={{ all_host.id }}" class="btn btn-info btn-sm">推送</a></td>
                                   </tr>
                               {% endfor %}
                               </tbody>
                            </table>
                           {% endif %}
                            <div class="row">
                                 <div class="col-xs-6">
                                       <div class="dataTables_info" id="example_info" role="alert" aria-live="polite" aria-relevant="all"></div>
                                 </div>
                                 <div class="col-xs-6">
                                      <div class="dataTables_paginate paging_simple_numbers" id="exmaple_paginate">
                                           <a href="http://www.mamicode.com/{% url ‘sousuo‘ %}" class="btn btn-info btn-sm" role="button">搜索</a>
                                           <a href="http://www.mamicode.com/{% url ‘sousuoh‘ %}" class="btn btn-info btn-sm" role="button">历史搜索</a>
                                           {% if all_host_list.has_previous %}
                                                <a href="http://www.mamicode.com/?page={{ all_host_list.previous_page_number }}">上一页</a>
                                           {% endif %}
                                           <span class="current">
                                                第{{ all_host_list.number }}页,共{{ all_host_list.paginator.num_pages }}页
                                           </span>
                                           {% if all_host_list.has_next %}
                                                <a href="http://www.mamicode.com/?page={{ all_host_list.next_page_number }}">下一页</a>
                                           {% endif %}
                                      </div>
                                 </div>
                           </div> <!--row end-->
                         </div>
                     </div>
                 </div>
                 <div style = "text-align: center">
                 <button type="submit" class="btn btn-primary">全部更新</button>
                </div>
</form>
{% endblock %}

本文出自 “学习linux” 博客,请务必保留此出处http://10265013.blog.51cto.com/10255013/1909955

django部分代码