sveetch / djangocodemirror Goto Github PK
View Code? Open in Web Editor NEWDjango CodeMirror is a Django application to embed CodeMirror
Home Page: http://djangocodemirror.readthedocs.io/
License: MIT License
Django CodeMirror is a Django application to embed CodeMirror
Home Page: http://djangocodemirror.readthedocs.io/
License: MIT License
CSS sources should be builded from SCSS sources compiled by Compass.
Do not embed anymore the old jquery.cookie plugin, instead use the recent one that is also shipped within Foundation5 assets
Trying to change a CODEMIRROR_MODES.update config and use it throws and error. Here is the traceback:
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 199, in render
return self.nodelist.render(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/loader_tags.py", line 70, in render
result = block.nodelist.render(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 1050, in render
return render_value_in_context(output, context)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/template/base.py", line 1028, in render_value_in_context
value = force_text(value)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/utils/encoding.py", line 78, in force_text
s = six.text_type(s)
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/utils/html.py", line 382, in
klass.unicode = lambda self: mark_safe(klass_unicode(self))
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/forms/widgets.py", line 59, in str
return self.render()
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/forms/widgets.py", line 62, in render
return mark_safe('\n'.join(chain(*[getattr(self, 'render' + name)() for name in MEDIA_TYPES])))
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/forms/widgets.py", line 69, in render_js
) for path in self._js
File "/home/syn/dev/test/mogo64/lib/python2.7/site-packages/django/forms/widgets.py", line 89, in absolute_path
if path.startswith(('http://', 'https://', '/')):
AttributeError: 'dict' object has no attribute 'startswith'
Rename exceptions to contain 'Exception' or 'Error' to be more explicit (and not confused with other objects).
Iโm working with Django 1.10.7 and Python 3.5
My code:
from djangocodemirror.widgets import CodeMirrorAdminWidget
@admin.register(FlatPage)
class FlatPageAdmin(FlatPageAdmin):
formfield_overrides = {
models.TextField: {'widget': CodeMirrorAdminWidget(config_name='html')},
}
Internal Server Error: /admin/flatpages/flatpage/1/change/
Traceback (most recent call last):
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/core/handlers/exception.py", line 42, in inner
response = get_response(request)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/response.py", line 109, in render
self.content = self.rendered_content
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/response.py", line 86, in rendered_content
content = template.render(context, self._request)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 208, in render
return self._render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/test/utils.py", line 94, in instrumented_test_render
return self.nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/test/utils.py", line 94, in instrumented_test_render
return self.nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/loader_tags.py", line 174, in render
return compiled_parent._render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/test/utils.py", line 94, in instrumented_test_render
return self.nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/loader_tags.py", line 70, in render
result = block.nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/loader_tags.py", line 70, in render
result = block.nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/loader_tags.py", line 210, in render
return template.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 210, in render
return self._render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/test/utils.py", line 94, in instrumented_test_render
return self.nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/defaulttags.py", line 315, in render
return nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/defaulttags.py", line 315, in render
return nodelist.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 994, in render
bit = node.render_annotated(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 961, in render_annotated
return self.render(context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 1050, in render
return render_value_in_context(output, context)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/template/base.py", line 1028, in render_value_in_context
value = force_text(value)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/utils/encoding.py", line 76, in force_text
s = six.text_type(s)
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/utils/html.py", line 391, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/forms/boundfield.py", line 43, in __str__
return self.as_widget()
File "/opt/envs/my_project/lib64/python3.5/site-packages/django/forms/boundfield.py", line 101, in as_widget
return force_text(widget.render(name, self.value(), attrs=attrs))
File "/opt/envs/my_project/lib64/python3.5/site-packages/djangocodemirror/widgets.py", line 124, in render
html.append(self.codemirror_script(final_attrs))
File "/opt/envs/my_project/lib64/python3.5/site-packages/djangocodemirror/widgets.py", line 88, in codemirror_script
opts = self.codemirror_config()
File "/opt/envs/my_project/lib64/python3.5/site-packages/djangocodemirror/widgets.py", line 72, in codemirror_config
return self.editor_manifest.get_codemirror_parameters(self.config_name)
File "/opt/envs/my_project/lib64/python3.5/site-packages/djangocodemirror/manifest.py", line 220, in get_codemirror_parameters
for k, v in config.items():
RuntimeError: dictionary changed size during iteration
Try to see to upgrade to a more recent CodeMirror version, actually it's the 3.0, at least the last 3.x version. 4.0 will be nice but its seems to have too many changes that can affect the djangocodemirror code.
Fields does not have a method to define medias (css, js), it have been commented long time ago in favor of template tags usage.
But we need this method to automatically have Codemirror on model fields within the admin, without to override templates for the model.
I am trying to render a TextField on the admin portal that highlights the syntax for html using the CodeMirrorWidget though nothing is rendering. I have followed the instructions in the docs:
settings.py:
from djangocodemirror.settings import *
CODEMIRROR_MODES.update({
'xml': {
'modes': ['html'],
'htmlMode': True,
'matchBrackets': True,
'extraKeys': {"Ctrl-Space": "autocomplete"},
'addons': [
"CodeMirror/addon/edit/matchbrackets.js",
"CodeMirror/addon/hint/show-hint.js",
"CodeMirror/addon/hint/css-hint.js",
],
'extra_css': [
"CodeMirror/addon/hint/show-hint.css",
],
},
})
admin.py
from django.contrib import admin
from django.db import models
from djangocodemirror.widgets import CodeMirrorWidget
class FrontendAdminSite(admin.AdminSite):
frontend_admin_site = FrontendAdminSite(name='frontend_admin')
@admin.register(Template, site=frontend_admin_site)
class TemplateAdmin(admin.ModelAdmin):
list_display = ("name", "template")
search_fields = ("name__startswith",)
formfield_overrides = {
models.TextField: {
'widget': CodeMirrorWidget(
attrs={
'rows': '10',
'cols': '100'
}
)
}
}
Despite the above, I am just getting a standard textfield on the admin page. What should I be doing to see the CodeMirror editor?
Within a Foundation environment i see that the preview window does not fit to the editor size.
Seems that there is some CSS added by Foundation that disturb the calcul of the preview window size.
Hi,
README suggests the following way of using djangocodemirror
within the admin site:
class MyModelAdmin(admin.ModelAdmin):
formfield_overrides = {
models.TextField: {'widget': CodeMirrorWidget(config_name='default', embed_settings=True)},
}
However, when I try to apply this as follows:
from django.db import models
from django.contrib import admin
from djangocodemirror.fields import CodeMirrorWidget
from reversion.admin import VersionAdmin
from .models import (
Template,
StyleSheet,
)
class StyleSheetAdmin(VersionAdmin):
list_display = ('name', 'active')
formfield_overrides = {
models.TextField: {'widget': CodeMirrorWidget(config_name='default', embed_settings=True)},
}
class TemplateAdmin(VersionAdmin):
list_display = ('name', 'active')
formfield_overrides = {
models.TextField: {'widget': CodeMirrorWidget(config_name='default', embed_settings=True)},
}
admin.site.register(Template, TemplateAdmin)
admin.site.register(StyleSheet, StyleSheetAdmin)
I get the following error:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/japsu/Hobby/tracontent/resources/admin.py", line 21, in <module>
class TemplateAdmin(VersionAdmin):
File "/Users/japsu/Hobby/tracontent/resources/admin.py", line 25, in TemplateAdmin
models.TextField: {'widget': CodeMirrorWidget(config_name='default', embed_settings=True)},
File "/Users/japsu/Hobby/venv-tracontent/lib/python2.7/site-packages/djangocodemirror/widgets.py", line 28, in __init__
super(CodeMirrorWidget, self).__init__(attrs=attrs, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'embed_settings'
The behaviour is the same using the newest djangocodemirror
from PyPI or GitHub HEAD. See models.py
here.
Following sveedocuments et DjangoSveetchies, this app need to implement some asset bundles to use with django-assets.
Actually djangocodemirror load a lots of assets, it would speed up load with django-assets.
Keyboard shortcut are not really usefull actually because they involve complex key combo and not described in the button bubble help.
Would be nice to expose a helper to use to easily add/remove/update some parameters on default CODEMIRROR_SETTINGS
.
Signature would be something like that:
codemirror_settings_update(configs, parameters_dict, on)
A sample usage would be:
CODEMIRROR_SETTINGS = codemirror_settings_update(CODEMIRROR_SETTINGS, {
'lineNumber': False,
'indent': 4
}, on=['css', 'python'])
We should be able to disable asset bundle for some configuration.
Seems the more simplest and natural way would be to simply set bundle names to None
.
I had a bug while using your library in StackedInline admin forms.
It was caused because the ID generated by django included some hyphen. As you know your library use the id to set the var name in the CODEMIRROR_FIELD_INIT_JS , and we cannot use hyphens in variables names in javascript..
I was getting this error in the console
Uncaught SyntaxError: Unexpected token -
Here is the cause of the problem:
https://github.com/sveetch/djangocodemirror/blob/master/djangocodemirror/settings.py#L14
# Template string for HTML Code to instanciate CodeMirror for a field.
CODEMIRROR_FIELD_INIT_JS = (u"""<script>var {varname} = """
"""CodeMirror.fromTextArea("""
"""document.getElementById("{inputid}"),"""
"""{settings});</script>""")
https://github.com/sveetch/djangocodemirror/blob/master/djangocodemirror/widgets.py#L85
varname = "{}_codemirror".format(inputid)
html = self.get_codemirror_field_js()
opts = self.codemirror_config()
return html.format(varname=varname, inputid=inputid,
settings=json.dumps(opts, sort_keys=True))
An easy fix for me was to remove the var name from the CODEMIRROR_FIELD_INIT_JS
like this:
# Template string for HTML Code to instanciate CodeMirror for a field.
CODEMIRROR_FIELD_INIT_JS = (u"""<script>"""
"""CodeMirror.fromTextArea("""
"""document.getElementById("{inputid}"),"""
"""{settings});</script>""")
I don't really need to store the codemirror instance in a variable that I don't use so it works for me.
Thank you very much for the library ๐
Since the update of the git submodule of CodeMirror, the editor is broken, there is Javascript errors and features does not work anymore.
There is some CodeMirror options and method that has changed, and also some HTML.
This library is too heavy and not really stable, also this is too big for the little usage done with it.
So drop his usage, use the internal panel system from CodeMirror to display a simple message row.
There is a lot of notifications in editor that actually do nothing because of qtip2 deprecation.
This is bad because quicksave action need to notify about request successes/fails, actually the user will not see anything about the quicksave.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.