首页 > 代码库 > 在线编辑代码[django]版本

在线编辑代码[django]版本

再国内,做什么都这么吃力。连aliyun 的ssh 都被封这是什么世道,所以做一个在线编辑代码的
忙忙碌碌有点粗糙。大家见谅
?1. [代码]views.py     

#-*- coding:utf-8 -*- 
# jQuery File Tree
# Python/Django connector script
# By Martin Skou
#
import os
import urllib
from django.http import HttpResponse
from django.shortcuts import render_to_response
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
 
def index(request):
    if not request.user.is_superuser:
       return HttpResponse(u"不要乱来")
    return render(request, ‘codemirror/index.html‘, {"foo": "bar"},
        content_type="text/html")
          
 
def openfile(request):
    if not request.user.is_superuser:
       return HttpResponse(u"不要乱来")
    path = request.GET[‘path‘]
     
    f = open(path)
    con = f.read()
    f.close()
     
    return render(request, ‘codemirror/openfile.html‘, {"con": con, "path":path},
        content_type="text/html")
 
@csrf_exempt
def savefile(request):
    if not request.user.is_superuser:
       return HttpResponse(u"不要乱来")
    path = request.POST[‘path‘]
    con = request.POST[‘con‘]
     
    f = open(path, ‘wr‘)
    f.write(con.encode(‘utf-8‘))
    f.close()
     
    return HttpResponse(‘成功‘)
     
@csrf_exempt
def createfile(request):
    if not request.user.is_superuser:
       return HttpResponse(u"不要乱来")
    path = request.GET[‘path‘]
 
    f = open(path, ‘wr‘)
    f.write(‘‘)
    f.close()
 
    return HttpResponse(‘成功‘)
         
@csrf_exempt
def dirlist(request):
   if not request.user.is_superuser:
       return HttpResponse(u"不要乱来")
   r=[‘<ul class="jqueryFileTree" >‘]
   try:
       r=[‘<ul class="jqueryFileTree" >‘]
       d=urllib.unquote(request.POST.get(‘dir‘,‘c:\\temp‘))
       for f in os.listdir(d):
           ff=os.path.join(d,f)
           if os.path.isdir(ff):
               r.append(‘<li class="directory collapsed"><a href="http://www.mamicode.com/#" rel="%s/">%s</a></li>‘ % (ff,f))
           else:
               e=os.path.splitext(f)[1][1:] # get .ext and remove dot
               r.append(‘<li class="file ext_%s"><a href="http://www.mamicode.com/#" rel="%s">%s</a></li>‘ % (e,ff,f))
       r.append(‘</ul>‘)
   except Exception,e:
       r.append(‘Could not load directory: %s‘ % str(e))
   r.append(‘</ul>‘)
   return HttpResponse(‘‘.join(r))
2. [代码]urls.py     

#-*- coding:utf-8 -*-
from django.conf.urls.defaults import patterns, url
 
urlpatterns = patterns(‘codemirror.views‘,
    url(r‘^dirlist.html$‘, ‘dirlist‘),
    url(r‘^index.html$‘, ‘index‘),
    url(r‘^openfile.html$‘, ‘openfile‘),
    url(r‘^savefile.html$‘, ‘savefile‘),
    url(r‘^createfile.html$‘, ‘createfile‘),
     

3. [代码]index.html     

<html>
<head>
    <script type="text/javascript" src="http://www.mamicode.com/static/js/jquery-1.9.0.min.js"></script>
    <link href="http://www.mamicode.com/static/codemirror-3.16/lib/codemirror.css" rel="stylesheet">
    <script type="text/javascript" src="http://www.mamicode.com/static/codemirror-3.16/lib/codemirror.js"></script> 
     
    <script type="text/javascript" src="http://www.mamicode.com/static/jqueryFileTree/jqueryFileTree.js"></script>
    <link href="http://www.mamicode.com/static/jqueryFileTree/jqueryFileTree.css" rel="stylesheet">
     
    </head>
<body>
 
<h1>
 
 
<p>My first paragraph.</p>
 
<div id="file_tree_div_id" >
</div>
<iframe id="editor_iframe_id" src="" ></iframe>
<script type="text/javascript">
  
$(document).ready( function() {漫画图片
    /* $.get(‘/codemirror/dirlist.html?dir=/home/boneyao/‘, function(html){
        $(‘#file_tree_div_id‘).html(html);
    });*/http://www.bizhizu.cn/manhua/?
     
    $(document).ready( function() {
        $(‘#file_tree_div_id‘).fileTree({
            root: ‘/home/boneyao/‘,
            script: ‘/codemirror/dirlist.html?dir=/home/boneyao/‘,
            expandSpeed: 1000,
            collapseSpeed: 1000,
            multiFolder: false
        }, function(file) {
           $(‘#editor_iframe_id‘).attr(‘src‘, "http://www.boneyao.com/codemirror/openfile.html?path="+file);
    });
});
});
 
   /*
  var editor = CodeMirror.fromTextArea(myTextarea, {
    mode: "text/html"
  });*/
 
 
</script>
 
</body>
</html>
4. [代码]openfile.html     
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script type="text/javascript" src="http://www.mamicode.com/static/js/jquery-1.9.0.min.js"></script>
    <link href="http://www.mamicode.com/static/codemirror-3.16/lib/codemirror.css" rel="stylesheet">
    <script type="text/javascript" src="http://www.mamicode.com/static/codemirror-3.16/lib/codemirror.js"></script> 
    <script type="text/javascript" src="http://www.mamicode.com/static/codemirror-3.16/mode/python/python.js"></script> 
    <script type="text/javascript" src="http://www.mamicode.com/static/codemirror-3.16/mode/javascript/javascript.js"></script>
     
    <script type="text/javascript" src="http://www.mamicode.com/static/jqueryFileTree/jqueryFileTree.js"></script>
    <link href="http://www.mamicode.com/static/jqueryFileTree/jqueryFileTree.css" rel="stylesheet">
     
    </head>
<body>
<input type="button" value="http://www.mamicode.com/保存" onclick="save()"/></h1>
<TextArea id="myTextarea" >
{{con}}
</TextArea>
<script type="text/javascript">
var mode = "";
 
switch(window.location.href.split(‘.‘).slice(-1)[0]){
    case ‘py‘: mode=‘python‘;break;
    case ‘js‘: mode=‘javascript‘;break;
}
var editor = CodeMirror.fromTextArea($(‘#myTextarea‘)[0], {
    mode: mode
});
editor.setSize(‘100%‘,‘100%‘);
 
 
function save(){
    var value = http://www.mamicode.com/editor.getValue();
     
    $.post(‘http://www.boneyao.com/codemirror/savefile.html‘, {
        ‘path‘:"{{path}}",‘con‘:value
    }, function(text){
        alert(text);
    });
};
</script>
 
</body>
</html>
?
?

5. [图片] 2.bmp    

在线编辑代码[django]版本