2014-04-01 74 views
2

我想創建一個帶有多個TinyMCE編輯器實例的HTML頁面。編輯人數因要求而異;所以我不能枚舉它們並單獨初始化它們。這裏是我的代碼:具有多種形式的Django-TinyMCE

views.py:

from tinymce.widgets import TinyMCE 
class ThreadForm(forms.Form): 
    subject = forms.CharField(max_length=300, widget=forms.TextInput(attrs={'size':'100'})) 
    body = forms.CharField(widget=TinyMCE()) 
class MessageForm(forms.Form): 
    thread_pk = forms.IntegerField() 
    body = forms.CharField(widget=TinyMCE()) 

urls.py:

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

settings.py:

INSTALLED_APPS = (
    ... 
    'tinymce', 
) 
... 
TINYMCE_DEFAULT_CONFIG = { 
    'selector': 'textarea', 
    'theme': 'advanced', 
    'width': 600, 
    'height': 300, 
    'theme_advanced_toolbar_location': 'top', 
    'theme_advanced_buttons1': 'bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,outdent,indent,hr,|,undo,redo', 
    'theme_advanced_buttons2': 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,link,unlink,charmap,|,visualaid,table,|,blockquote,sub,sup,|,preview,code,emotions,image', 
    'theme_advanced_buttons3': '', 
    'plugins': 'paste,table,spellchecker,searchreplace,emotions', 
    'theme_advanced_resizing': True, 
} 

member_forums.html:

... 
{% block headers %} 
{{ thread_form.media }} 
{% endblock %} 
... 
<table id="new_thread_table"> 
    {{ thread_form.as_table }} 
</table> 
... 
{% for message_form in message_forms %} 
    <table class="new_message_table"> 
     {{ message_form.as_table }} 
    </table> 
    ... 
{% endfor %} 

模板中有一個ThreadForm和多個MessageForms。

當我在HTML中註釋掉MessageForms時,ThreadForm似乎可以工作,但是當我取消註釋它們時,ThreadForm將加載一個不更新的TinyMCE外觀(添加文本不會使撤消按鈕顯示爲啓用即使它是),並且當我提交表單時,表單的正文條目丟失,導致form.is_valid失敗。

我只有{{ thread_form.media }}在我的模板頭,沒有消息框。迭代通過:

{% for message_form in message_forms %} 
    {{ message_form.media }} 
{% endfor %} 

也沒有做任何好處,要麼。

做一些研究之後,似乎TinyMCE的被初始化時MessageForms加載次數過多,導致丟失數據表單提交期間(從頂答案編輯到:TinyMCE with Django: "This field is required"

我迷路瞭如何讓這個工作。任何幫助或指針,將不勝感激。

+0

註釋掉thread_form使得第一個message_form與TinyMCE外觀一起出現,在嘗試提交數據時沒有功能和相同的錯誤。 – Nagra

+0

在頂部添加了一個簡單地調用tinyMCE.init({theme:「advanced」,})的腳本;當其他表單被註釋掉時沒有什麼區別。所以也許問題不是init被多次調用?似乎問題是隻有一個以上的TinyMCE字段。 – Nagra

回答

-1

TinyMCE使用id來指定哪個textarea應該是TinyMCE,並且id應該是HTML中唯一的。由於我的身體都被稱爲身體,Django的form.as_table使用相同的身份證明。當TinyMCE試圖將編輯器分配給該ID時,它在到達具有相同ID的第二個字段時失敗。類似的問題:

TinyMCE not working when loading two textareas

我改變了對MessageForm.body MessageForm.body1,這讓像一個魅力的ThreadForm是否工作正常?現在我需要遍歷並更改每個MessageForm的ID。但是應該有更好的解決方案。 TinyMCE應該能夠渲染所有文本區域。也許完全刪除IDS?

+1

不,你可以在你的TinyMCE配置文件中使用'selector:textarea'來改變它。 – xyres

+0

編輯答案並添加更多代碼來提問。 – Nagra

+0

我會嘗試在settings.py中的默認TinyMCE配置變量中設置'selector':'textarea',看看在嘗試我自己的解決方案之前是否有所作爲。 – Nagra

0

刪除了Django-TinyMCE並開始使用TinyMCE 4.0.21。現在它出色地工作。

views.py:

... 
class ThreadForm(forms.Form): 
    subject = forms.CharField(max_length=300, widget=forms.TextInput(attrs={'size':'100'})) 
    body = forms.CharField(widget=forms.Textarea(attrs={'class':'thread'})) 
... 

member_forums.html:

... 
{% block headers %} 
<script type="text/javascript" src="{% static 'tinymce/tinymce.min.js' %}"></script> 
<script type="text/javascript" src="{% static 'tinymce/threads.js' %}"></script> 
{% endblock %} 

{% block content %} 
    ... 
    <form id="new_thread_form" method="post" action=""> 
     {% csrf_token %} 
     <table id="new_thread_table"> 
      {{ thread_form.as_table }} 
     </table> 
    </form> 
    ... 
    <form class="new_message_form" method="post" action=""> 
    {% csrf_token %} 
    {% for message_form in message_forms %} 
     {{ message_form.thread_pk }} 
     <textarea class="message" name="body"></textarea> 
    {% endfor %} 
    ... 
{% endblock %} 

線程。js:

tinyMCE.init({ 
    selector: "textarea.thread", 
    theme: "modern", 
    ... 
}); 

tinyMCE.init({ 
    selector: "textarea.message", 
    theme: "modern", 
    ... 
});