用于用浏览器内代码编辑器codemrror2替换文本区域的django小部件
django-codemirror2的Python项目详细描述
Django小部件,用于将文本区域替换为浏览器内代码编辑器codemarror。 在Django1.6+上测试,支持Python2.7和Python3.4+。
安装
- 运行pip install django-codemirror2
- 将codemirror2添加到已安装的应用程序中
- 收集静态文件:python manage.py collectstatic
要直接从git使用django-codemirror2,需要初始化 通过运行git submodule init && git submodule update,代码镜像子模块。
用法
from django import forms from codemirror2.widgets import CodeMirrorEditor class TestForm(forms.Form): css = forms.Charfield(widget=CodeMirrorEditor(options={'mode': 'css'}))
options参数将作为json传递给CodeMirror.fromTextArea,请参见 http://codemirror.net/manual.html#config用于可能的值。不通过用户控制 数据作为选项,因为这可能导致XSS漏洞。
如果要使用依赖于其他模式的模式,例如htmlmixed,则 还需要通过传递modes参数来加载依赖项:
html = forms.Charfield(widget=CodeMirrorEditor(modes=['css', 'xml', 'javascript', 'htmlmixed'], options={'mode': 'htmlmixed'}))
如果要自定义用于初始化代码镜像编辑器的javascript,请使用script_template:
foo = forms.Charfield(widget=CodeMirrorEditor(options={'mode': 'xml'}, script_template='some/template.html'))
您可以将脚本模板基于包含的模板codemirror_script.html。
示例应用程序
其中包括一个简单的示例应用程序。运行它:
- 运行tox -e devenv
- 运行./run_example_server.sh
- 在浏览器中访问http://localhost:8000/admin/testapp/。