阿小信大人的头像
Where there is a Python, there is a way. 阿小信大人

Django中使用TinyMCE和代码高亮2014-09-02 06:22

Admin中添加TinyMCE

1 下载tinymce_3.5.2.zip,解压后把tinymce文件夹复制到项目的静态文件js的目录。

2 在tinymce文件夹下新建tiny_mce_config.js,内容为

tinyMCE.init({
    mode : "textareas",
    theme : "advanced",

    plugins : "autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

    theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",

    theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",

    theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",

    theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
    theme_advanced_toolbar_location : "top",
    theme_advanced_toolbar_align : "left",
    theme_advanced_statusbar_location : "bottom",
    theme_advanced_resizing : true,
    skin : "o2k7",
    skin_variant : "silver",
    content_css : "css/example.css",
    template_external_list_url : "js/template_list.js",
    external_link_list_url : "js/link_list.js",
    external_image_list_url : "js/image_list.js",
    media_external_list_url : "js/media_list.js",
    template_replace_values : {
            username : "Some User",
            staffid : "991234"
    }
});

3 在models的Admin中加入

class BlogPostAdmin(admin.ModelAdmin):

    class Media:
        js = [
            '/js/tiny_mce/tiny_mce_src.js', 
            '/js/tiny_mce/tiny_mce_config.js',
        ]

代码高亮

1 下载syntaxhighlighter 3.0.83,解压复制到Django的静态目录

2 下载syntaxhl,复制到tinymce的plugin目录下

3 在tinymce的配置文件中在plugins里面加个syntaxhl,在按钮栏上也加

4 在base模板中调用js和css,如下

<link href="/js/syntaxhighlighter/styles/shCore.css" type="text/css" rel="stylesheet"/>
<link href="/js/syntaxhighlighter/styles/shThemeDjango.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shCore.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushPython.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushXml.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushSql.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushJScript.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushJava.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushCss.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushCpp.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushPerl.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushBash.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushCSharp.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushPhp.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shBrushPerl.js"></script>
<script type="text/javascript" src="/js/syntaxhighlighter/scripts/shCore.js"></script>
<script  type = "text/javascript">
    SyntaxHighlighter.all();
</script>

SyntaxHighlighter.all();一定要加.3.083版本不需要加那个什么swf文件。

关于在models中加载css的话,要用字典方式 https://docs.djangoproject.com/en/dev/topics/forms/media/

class Media:
    css = { 'all':('test1.css','test2.css'), }

如果您觉得从我的分享中得到了帮助,并且希望我的博客持续发展下去,请点击支付宝捐赠,谢谢!

若非特别声明,文章均为阿小信的个人笔记,转载请注明出处。文章如有侵权内容,请联系我,我会及时删除。

#Python#   #django
分享到:
阅读[3583] 评论[0]

上一篇:凯撒密码
下一篇:quiz小程序

你可能也感兴趣的文章推荐

本文最近访客

发表评论