django-ckeditor to Django admin

若如初见. 提交于 2019-12-23 05:59:29

问题


I've installed django-ckeditor on django 1.5 as instructed in the docs. I've changed TextField in my applications models.py to RichTextField as said in the docs. However I still see blank textarea in the Django admin instead of the ckeditor. This question was asked 3 years ago and none of the answers worked for me. The ckeditor.js loads just fine when I get the page. Any suggestions? my app name is newsfeed.

models.py:

from cms.models.pluginmodel import CMSPlugin
from cms.models import Page
from django.db import models
from time import time
from ckeditor.fields import RichTextField

def get_upload_file_name(instance, filename):
    return "uploaded_files/%s_%s" % (str(time()).replace('.','_'),filename)


# Create your models here.
class NewsFeed (models.Model):
    title = models.CharField(('Feed Name'),max_length=200,help_text=('Feed name is visible only in edit mode'))
    publisher = models.CharField(('Publisher'),max_length=200)

    def __unicode__(self):
        return self.title

    def get_absolute_url(self):
        return "/newsfeed/get/%i" % self.id

class NewsItem(models.Model):
    feed_id = models.ForeignKey(NewsFeed)
    title = models.CharField(('Title'),max_length=200)
    subtitle = models.CharField(('Sub-Title'),max_length=350,help_text=('Subtitles are displayed in auto-scroller and has max characters of 350'))
    #body = models.TextField(('Content'),blank=True,help_text=('Content is NOT visible in auto-scroller !'))
    body = RichTextField()      
    url = models.URLField(("Link"), blank=True, null=True)
    page_link = models.ForeignKey(Page, verbose_name=("page"), blank=True, null=True, help_text=("A link to a page has priority over a text link."))
    pub_date = models.DateTimeField('Publish Date')
    is_published = models.BooleanField(('Published'), default=False)


class NewsFeedPlugin(CMSPlugin):
    newsfeed = models.ForeignKey(NewsFeed)

admin.py:

from django.contrib import admin
from newsfeed.models import NewsFeed,NewsItem
from cms.admin.placeholderadmin import PlaceholderAdmin


class NewsItemInline(admin.StackedInline):
    model = NewsItem
    extra=0

class NewsFeedAdmin(admin.ModelAdmin):
    inlines = [NewsItemInline]
    class Media:
       js = ('ckeditor/ckeditor/ckeditor.js')
admin.site.register(NewsFeed,NewsFeedAdmin)

config.js from ckeditor:

/**
 * @license Copyright (c) 2003-2013, CKSource - Frederico Knabben. All rights reserved.
 * For licensing, see LICENSE.html or http://ckeditor.com/license
 */

CKEDITOR.editorConfig = function( config ) {
    // Define changes to default configuration here. For example:
    // config.language = 'fr';
    // config.uiColor = '#AADC6E';
};

回答1:


The easiest way to do this would be by making use of class Media of Django's ModelAdmin.

Suppose you have an Article model with a TextField to which you wish to add ckeditor. Assuming you have kept the ckeditor.js file inside your project's static folder, the following is what you should do:

admin.py

class ArticleAdmin(admin.ModelAdmin):
    ...
    # your code here 
    ...

    class Media:
        js = ('ckeditor.js',)
        # do not write '/static/ckeditor.js' as Django automatically looks 
        # in the static folder

That's it.

If you wish to add configuration files for ckeditor or any more JavaScript files, just do this:

js = ('ckeditor.js', 'configuration-ckeditor.js')

In case you wish to add TinyMCE instead of ckeditor, I have a repository on GitHub to make it easier: https://github.com/xyres/django-tinymce-noapp

Update

After looking at your configuration file, I am certain the problem is not in Django but in your config file.

You need to tell CKEditor to which field to add the editor to. So, delete everything from your configuration file and add only this one line:

CKEDITOR.replace('textarea');

This would replace the textarea with an editor.

Don't forget to add this config file to the class Media.




回答2:


you may follow this step of github-django ckeditor and it will run succesfully with ckeditor as output. I have verified by making the models and testing it: link:https://github.com/django-ckeditor/django-ckeditor



来源:https://stackoverflow.com/questions/27021615/django-ckeditor-to-django-admin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!