Use differents configs in CKEDITOR with Django - django

In my Django back end, I have a full toolbar.
However, I want to propose Ckeditor to my users with a minimum of functionnalities.
The problem is, I don't know how to this.
I tried to override my config :
<script type="text/javascript">
CKEDITOR.replace( 'default',
{
toolbar : 'Basic',
});
</script>
But nothing happened, even after removing my browser cache.
This is my Django settings :
CKEDITOR_BASEPATH = "/static/ckeditor/ckeditor/"
CKEDITOR_JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'full',
'height': 500,
'width': 1500,
},
}

Maybe it is not working because in your Django code the settings "CKEDITOR_CONFIGS" for toolbar defined as full.
Also, you can configure toolbar through online builder - https://ckeditor.com/docs/ckeditor4/latest/guide/dev_plugins.html.

Related

How to resize django ckeditor inside the form model

I need to use django-ckeditor in various part of my project, so for each part need to resize it.
But I know that I can resize it by adding this definitions in settings as below:
INSTALLED_APPS = [
'ckeditor',
]
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'Basic',
'height': 70,
'width': 430,
},
}
however, as I mentioned above, I am using several editor in different parts, so need to resize them differently.
How to reside in different form.fields?
class proForm(forms.ModelForm):
description = forms.CharField(widget=CKEditorWidget())
class Meta:
fields = ('__all__')
This raises error:
description = forms.CharField(widget=CKEditorWidget({
'toolbar': 'Basic',
'height': 70,
'width': 430,
}))
Have a look at https://github.com/django-ckeditor/django-ckeditor#optional-customizing-ckeditor-editor
It seems you can define multiple set of editors and give config_name when instantiating the field.

Django CKEditor 404 image not found

On my Django admin panel, I can upload an image using CKEditor. However, the image doesn't apear and it return a 404 not found on the image file path.
Actually, my image files are available on a dedicated url : media.mysite.com
But, CKEditor is trying to GET the file from mywebsite.com/media/uploads/..., so this is normal that I get a 404 error.
What is the good parameter to do for telling to CKEditor to use media.mysite.com to get any images ?
Settings.py
MEDIA_ROOT = "/var/www/media/mysite/"
MEDIA_URL = "/media/"
CKEDITOR_BASEPATH = "/static/ckeditor/ckeditor/"
CKEDITOR_JQUERY_URL = 'https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'
CKEDITOR_UPLOAD_PATH = 'uploads/'
CKEDITOR_IMAGE_BACKEND = "pillow"
CKEDITOR_CONFIGS = {
'default': {
'toolbar': 'full',
'height': 300,
'width': '100%',
},
}
I visited many topics, but I found no answer. Thank you.
I solved my issue by adding an alias in my apache vhost :
Alias /media/ /var/www/media/
So, when CKEDITOR is trying to access on mywebsite.com/media/upload, it's redirected on a specific folder "/var/www/media/".
This is a good workaround

django-tinymce plugins not loading django 1.8

I'm trying to implement django-tinymce in my project. I'd love to use some rich text capability when writing my blog posts, so I'm aiming at applying the HTMLField to the body in the admin.
The settings that I am currently using are really simple - this is what I have in settings.py:
TINYMCE_DEFAULT_CONFIG = {
'theme': "advanced",
'plugins': "wordcount,preview,emotions,", //only wordcount seems to have any effect
'height': "400px",
'width': "700px",
}
this is in the models.py:
from tinymce import models as tinymce_models
...
body = tinymce_models.HTMLField()
and I call the .js in the heads like so:
<script type="text/javascript" src="{% static "tiny_mce/tiny_mce.js" %}"></script>
My issue - no matter what I do in the settings, I get the same result:
Here is what I got
I would appreciate any pointers to what I might be doing wrong.
Thanks a bunch!
Deyan
So, after banging my head for a few days, this is what I finally achieved.
settings.py
# tinymce
TINYMCE_DEFAULT_CONFIG = {
'theme': "advanced",
'plugins': "wordcount,preview,emotions,preview,spellchecker,",
'height': "400px",
'width': "700px",
'theme_advanced_buttons3' : "fontselect,fontsizeselect,emotions,preview,",
}
models.py
from tinymce.models import HTMLField
...
body = HTMLField()
result:
As you can see, smilies are smiling beautifully, I've got control over font family and size, it's looking really ugly, but it works and this is the price you pay for hacking stuff I suppose. But it works!
I found this list of plugins and buttons really helpful - what I wasn't getting before was that the plugins you only load into your django app, but in order to use them, you need to call their buttons. Really straightforward once you get it, but there you go.
Thanks!

django-summernote image upload

I recently implemented django-summernote with my forms, which works well for text. However, I struggle to get exactly how image upload works. Does anyone have some input on how it is done?
Problem
When choosing an image from file with Summernote, the Insert Image button is deactivated (works fine for image links). I did not write a custom 'upload_to' function, but as I get it, this is already done in django-summernote.
Details
Installed django-summernote according to documentation.
Added summernote to urls and in INSTALLED_APPS
Added summernote to my form field
directions = forms.CharField(
widget=SummernoteInplaceWidget(attrs={'maxlength':'4000'}),
required=False,
)
Also added some config in SUMMERNOTE_CONFIG (settings.py)
SUMMERNOTE_CONFIG = {
'iframe': True,
'airMode': True,
'width': '100%',
'height': '300',
'toolbar': [
# ['style', ['style']],
['font', ['bold', 'italic', 'underline', 'superscript', 'subscript', 'strikethrough', 'clear']],
# ['fontname', ['fontname']],
['fontsize', ['fontsize']],
# ['color', ['color']],
['para', ['ul', 'ol', 'paragraph']],
['height', ['height']],
['table', ['table']],
['insert', ['link', 'picture', 'video', 'hr']],
['view', ['fullscreen', 'codeview']],
['help', ['help']],
], }
Do I also have to write my own backend for attachments (images)? STATIC_URL and MEDIA_URL is defined in my settings.py, if that matters for this issue.
Update November 29th 2014:
When choosing an image, the following error is given in the console: "undefined is not a function", which is related to
imageInput.fileupload();
The "Insert Image" button is disabled.
As my project is in developing mode, I have DEBUG=True in my settings.
My urls look like:
urlpatterns += patterns('',
url(r'^summernote/', include('django_summernote.urls')),
) + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
and Media_root and Media_url are set to:
MEDIA_ROOT = path.join(path.dirname(__file__), 'media',)
MEDIA_URL = '/media/'
I use picture uploads outside django-summernote with these settings.
Feels like I am missing something, but canĀ“t see what.
Thanks in advance.
django-summernote shipped with backend support for image uploading out of box. So you don't have to write your own backend for it. MEDIA_ROOT or MEDIA_URL settings may have wrong value for uploading - permission problem or not valid path.
Run django project with runserver and please check the browser console(inspect) and python console after trying to upload an image.
And also refer Need a minimal Django file upload example for handling files on django project.

How to setup TINYMCE to point to static_files?

I am using Django 1.5.1, and as we know since Django 1.3 the world of Media and statis files have been separated for good reasons.
To my surprise django-tinymce's documentation is referring to how TINYMCE_JS_URL is pointing by default to the media url.
TINYMCE_JS_URL (default: settings.MEDIA_URL + 'js/tiny_mce/tiny_mce.js')
That doesn't make much sense. As in Django 1.3+ we have the static_url for self hosted js and css files. But trying to change that is confusing and doesn't work.
This is how I usually setup my static files settings:
STATIC_ROOT = '/home/kave/project-env/site/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = ('/home/kave/project-env/site/static_files/',)
In the static_files directory I have extracted the TINYMCE zipfile:
e.g. the path is like this:
/home/kave/project-env/site/static_files/tinymce/js/tinymce/tinymce.min.js
Then I have set the settings like this:
TINYMCE_JS_URL = STATIC_URL + 'tinymce/js/tinymce/tinymce.min.js'
TINYMCE_JS_ROOT = STATIC_ROOT + 'tinymce/js/tinymce'
However when the app runs, I see a plain textfield instead of TINYMCE.
WHat could I have overlooked please?
I have finally figured it out, hope it helps somebody else.
You need to point to the built-in tiny_mce that is automatically installed through pip.
No need to download anything.
<script type="text/javascript" src="{{ STATIC_URL }}tiny_mce/tiny_mce.js"></script>
For admin screen, make sure you have this:
class TinyMCEAdmin(admin.ModelAdmin):
class Media:
js = ('/static/tiny_mce/tiny_mce.js', )
admin.site.register(MyModel, TinyMCEAdmin)
And don't forget the config in settings.py
TINYMCE_DEFAULT_CONFIG = {
# General options
'mode' : "textareas",
'theme' : "advanced",
'plugins' : "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,wordcount,advlist,autosave",
# Theme options
'theme_advanced_buttons1' : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,", #fullscreen,code",
'theme_advanced_buttons2' : "bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,|,forecolor,backcolor",
#'theme_advanced_buttons3' : "tablecontrols,|,hr,sub,sup,|,charmap",
'theme_advanced_toolbar_location' : "top",
'theme_advanced_toolbar_align' : "left",
'theme_advanced_statusbar_location' : "bottom",
'theme_advanced_resizing' : 'true',
#Example content CSS (should be your site CSS)
#content_css : "/css/style.css",
'template_external_list_url' : "lists/template_list.js",
'external_link_list_url' : "lists/link_list.js",
'external_image_list_url' : "lists/image_list.js",
'media_external_list_url' : "lists/media_list.js",
# Style formats
'style_formats' : [
{'title' : 'Bold text', 'inline' : 'strong'},
{'title' : 'Red text', 'inline' : 'span', 'styles' : {'color' : '#ff0000'}},
{'title' : 'Help', 'inline' : 'strong', 'classes' : 'help'},
{'title' : 'Table styles'},
{'title' : 'Table row 1', 'selector' : 'tr', 'classes' : 'tablerow'}
],
'width': '700',
'height': '400'
}
Other than that make sure to use the HTMLField inside your model for any model textfield.
In forms.py, if you are not using modelform, you need to define the new widget as well.
To make work with django-tinymce
we have to configure the following
settings.py
INSTALLED_APPS = [
# .......
'tinymce',
# .......
]
TINYMCE_JS_URL = '%sjavascript/tiny_mce/' % (STATIC_URL)
urls.py
urlpatterns = [
# .....
url(r'^tinymce/', include('tinymce.urls')),
# .....
]