Django / TinyMCE 编辑器无法工作

3

我正在使用Python2.7的Django 1.8版本。我已经使用命令“pip install django-tinymce”安装了TinyMCE,并将'tinymce'应用程序添加到了INSTALLED_APPS中。

INSTALLED_APPS = (
    ...
    'tinymce',
    ...
)

项目URL中包含的内容: urls.py

url(r'^tinymce/', include('tinymce.urls')),

在Settings.py文件中配置TinyMCE

TINYMCE_JS_URL = os.path.join(STATIC_PATH,"django-tinymce-master/tinymce/media/tiny_mce/tiny_mce_src.js")


TINYMCE_JS_ROOT = os.path.join(STATIC_PATH, "django-tinymce-master/tinymce")

#D:\Dropbox\dp\p2d18\opinion\static\django-tinymce-master\tinymce

TINYMCE_DEFAULT_CONFIG = {
'plugins': "table,spellchecker,paste,searchreplace",
'theme': "advanced",
'cleanup_on_startup': True,
'custom_undo_redo_levels': 10,
}
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True

models.py

class Post(models.Model):
    ...
    body = models.TextField()
    ...

forms.py

from django import forms
from django.contrib.auth.models import User
from blog.models import Post, Comment, Tag
from tinymce.widgets import TinyMCE

class PostForm(forms.ModelForm):
    ---
    body = forms.CharField(widget=TinyMCE(attrs={'cols': 50, 'rows': 30}))
    ---

views.py

def add_post(request):
    if request.method == 'POST':
        form1Post = PostForm(request.POST)
        form2Tags = TagForm(request.POST)

        if form1Post.is_valid() and form2Tags.is_valid():
            post = form1Post.save()
            tag_title_from_form = form2Tags['tag_title'].value().strip().lower()
            tag_title_from_form = tag_title_from_form.rstrip(',')
            striped_tags = tag_title_from_form.split(',')
            for t in striped_tags:
                received_tag = t.strip()
                tag, created = Tag.objects.get_or_create(tag_title=received_tag)
                post.tag_set.add(tag)
            return index(request)
        else:
            print (form1Post.errors)
            print (form2Tags.errors)
    else:
        form1Post = PostForm()
        form2Tags = TagForm()

在我的base.html中添加了tinymce js链接

<script src="{% static 'django-tinymce-master/tinymce/media/tiny_mce/tiny_mce.js' %}"></script>

在我的add_post.html中添加了tinymce

{{ form1.media }}

文本区域的生成HTML

<textarea class="tinymce" cols="50" data-mce-conf="{&quot;cleanup_on_startup&quot;: true, &quot;spellchecker_languages&quot;: &quot;Afrikaans=af,Arabic=ar,Asturian=as,Azerbaijani=az,Bulgarian=bg,Belarusian=be,Bengali=bn,Breton=br,Bosnian=bs,Catalan=ca,Czech=cs,Welsh=cy,Danish=da,German=de,Greek=el,+English / Australian English / British,     English=en,Esperanto=eo,Spanish / Argentinian Spanish / Mexican Spanish / Nicaraguan Spanish / Venezuelan, Spanish=es,Estonian=et,Basque=eu,Persian=fa,Finnish=fi,French=fr,Frisian=fy,Irish=ga,Galician=gl,Hebrew=he,Hindi=hi,Croatian=hr,Hungarian=hu,Interlingua=ia,Indonesian=id,Ido=io,Icelandic=is,Italian=it,Japanese=ja,Georgian=ka,Kazakh=kk,Khmer=km,Kannada=kn,Korean=ko,Luxembourgish=lb,Lithuanian=lt,Latvian=lv,Macedonian=mk,Malayalam=ml,Mongolian=mn,Marathi=mr,Burmese=my,Norwegian Bokmal=nb,Nepali=ne,Dutch=nl,Norwegian, Nynorsk=nn,Ossetic=os,Punjabi=pa,Polish=pl,Portuguese / Brazilian Portuguese=pt,Romanian=ro,Russian=ru,Slovak=sk,Slovenian=sl,Albanian=sq,Serbian / Serbian Latin=sr,Swedish=sv,Swahili=sw,Tamil=ta,Telugu=te,Thai=th,Turkish=tr,Tatar=tt,Udmurt=ud,Ukrainian=uk,Urdu=ur,Vietn amese=vi,Simplified Chinese / Simplified Chinese / Traditional Chinese / Traditional Chinese=zh&quot;, &quot;elements&quot;: &quot;id_body&quot;, &quot;language&quot;: &quot;en&quot;, &quot;spellchecker_rpc_url&quot;: &quot;/tinymce/spellchecker/&quot;, &quot;directionality&quot;: &quot;ltr&quot;, &quot;theme&quot;: &quot;advanced&quot;, &quot;strict_loading_mode&quot;: 1, &quot;mode&quot;: &quot;exact&quot;, &quot;custom_undo_redo_levels&quot;: 10, &quot;plugins&quot;: &quot;table,spellchecker,paste,searchreplace&quot;}" data-mce-gz-conf="{&quot;themes&quot;: &quot;advanced&quot;, &quot;languages&quot;: &quot;en&quot;, &quot;debug&quot;: false, &quot;diskcache&quot;: true, &quot;plugins&quot;: &quot;table,spellchecker,paste,searchreplace&quot;}" id="id_body" name="body" rows="30"></textarea>

TinyMCE还未工作!我在哪里犯了错误?

data-mce-conf="{"..." - &quot; 应该替换为真正的引号 ' - xyres
@xyres 我是从浏览器(检查对象)中粘贴的。这是否意味着问题出在TinyMCE后端?我还尝试在HTML页面中将“"”替换为“'”,但未按所需格式响应。 - Muhammad Ahmed
你的HTML页面是否加载了TinyMCE JavaScript(即头部区域是否有指向它的链接)? - wobbily_col
是的,它确实可以。我已经添加了:<script src="{% static 'django-tinymce-master/tinymce/media/tiny_mce/tiny_mce.js' %}"></script> - Muhammad Ahmed
尝试使用 from tinymce.models import HTMLFieldbody = forms.HTMLField(widget=TinyMCE(attrs={'cols': 50, 'rows': 30})) - 这将处理Django管理小部件。 - lukeaus
2个回答

2

尝试在您的add_post.html页面上初始化tinyMCE字段

<script>
tinyMCE.init({
  mode: "textareas",  // to do all text areas
  // or
  selector: "#id_myfield",  // change this value according to your HTML
});
</script>

它已经生成了一个名为“init_tinymce.js”的JS文件,其中包含此代码([http://hastebin.com/uyupeyacax.lisp)。 - Muhammad Ahmed
@MuhammadAhmed,那个文件init__tinymce.js被调用了吗? - lukeaus
是的!我已经从浏览器窗口复制了文件。 - Muhammad Ahmed
你设置了TINYMCE_JS_URL和TINYMCE_JS_ROOT了吗?它们是什么? - lukeaus
我已经为JS文件设置了URL路径。根据此处找到的文档[https://readthedocs.org/projects/django-tinymce/downloads/pdf/tinymce4/]。然而,该文档将TinyMCE文件夹设置为“媒体”,而我使用了“静态”文件夹来实现此目的。 - Muhammad Ahmed
显示剩余2条评论

-1

我知道你已经解决了你的问题,但是当我在寻找答案来解决我的问题时,即在前端不显示我想要的内容,我回复你:

你只需要在前端进行过滤,所以在你的模板文件中添加如下内容:

{{ your_view_context.your_model_field | safe }}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接