python - Django/TinyMCE 编辑器不工作

标签 python django tinymce django-1.8 django-tinymce

我正在使用 Django 1.8 和 Python2.7。我已经使用命令“pip install django-tinymce”安装了 TinyMCE,并在 INSTALLED_APPS 中包含了“tinymcy”应用程序。

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

模型.py

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

表单.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 }}

Textarea 生成的 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 还没有工作!我在哪里犯了错误

最佳答案

试试这个来初始化 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>

关于python - Django/TinyMCE 编辑器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35955349/

相关文章:

python - 如何将多个 csv 文件中的行复制到 pandas 中的新文件?

django - 为数据迁移编写自动化测试

python - 从 django 应用程序下载一个文本/csv 文件,该文件从数据库查询中填充 HTML 表

javascript - jQuery 表单使用 TinyMCE 序列化 CodeIgniter 中缺少的数据

jquery-plugins - tinymce jquery插件错误tinymce不是函数

python - 尝试通过使用 h5py 更改索引字段类型来缩小 HDF5 文件的大小

python - 为什么可变变量不能在没有链接的情况下一起分配?

python - PO 中的 OpenERP V7 自定义

django - Collectstatic和S3 : not finding updated files

css - Tinymce Angular7 : Not loading custom css file via content_css property