django-ckeditor到Django管理员 [英] django-ckeditor to Django admin

查看:191
本文介绍了django-ckeditor到Django管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在django 1.5上安装了django-ckeditor,如 docs
我已经将我的应用程序models.py中的TextField更改为RichTextField,如文档中所述。
但是我仍然在Django管理员中看到空白textarea,而不是ckeditor。
这个问题是在3年前提出的,没有一个答案为我工作
当我得到页面时,ckeditor.js加载得很好。
任何建议?
我的应用程序名称是newsfeed。



models.py:

 从cms.models.pluginmodel导入CMSPlugin 
从cms.models导入页
从django.db导入模型
从时间导入时间
从ckeditor.fields导入RichTextField

def get_upload_file_name(实例,文件名):
返回uploaded_files /%s_%s%(str(time())。replace('。','_'),filename)


#在这里创建你的模型。
class NewsFeed(models.Model):
title = models.CharField(('Feed Name'),max_length = 200,help_text =('Feed name only visible 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 =('自动滚动,最大字符为350'))
#body = models.TextField(('Content'),blank = True,help_text =('内容在auto-croller!中不可见'))
body = RichTextField()
url = models.URLField((Link),blank = True,null = True)
page_link = models.ForeignKey(Page,verbose_name =(page ),bl ank = True,null = True,help_text =(一个页面的链接优先于文本链接)
pub_date = models.DateTimeField('Publish Date')
is_published = models。 BooleanField(('Published'),default = False)


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

admin.py:

  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.js')
admin.site.register(NewsFeed,NewsFeedAdmin)

来自ckeditor的config.js:

  / ** 
* @license版权所有(c)2003 -2013,CKSource - Frede克里布本版权所有。
*有关许可,请参阅LICENSE.html或http://ckeditor.com/license
* /

CKEDITOR.editorConfig = function(config){
/ /在此定义更改为默认配置。例如:
// config.language ='fr';
// config.uiColor ='#AADC6E';
};


解决方案

最简单的方法是使用的$ code>类媒体 Django的 ModelAdmin



假设你有一个文章模型,您要添加ckeditor的 TextField 。假设您在项目的静态文件夹中保存了 ckeditor.js 文件,则应该执行以下操作: / p>

admin.py

  class ArticleAdmin (admin.ModelAdmin):
...
#你的代码在这里
...

类媒体:
js =('ckeditor.js ',)
#不要写'/static/ckeditor.js',因为Django会在静态文件夹中自动查看

就是这样。



如果您想添加ckeditor或任何其他JavaScript文件的配置文件,只需执行以下操作: p>

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

如果您希望添加TinyMCE而不是ckeditor,我有一个GitHub上的存储库可以使其更容易: https://github.com/xyres/django-tinymce- noapp



更新



查看您的配置文件后,我确定问题不在Django,而是在您的配置文件中。



您需要告诉CKEditor将哪个字段添加到编辑器中。所以,删除您的配置文件中的所有内容,只添加以下一行:

  CKEDITOR.replace('textarea'); 

这将用编辑器替换textarea。



不要忘记将此配置文件添加到类Media


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';
};

解决方案

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.

这篇关于django-ckeditor到Django管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆