[英]TinyMCE with Django: “This field is required”
我目前在Django管理界面中使用TinyMCE編輯器時遇到問題。 將文本輸入兩個特定的TinyMCE字段並按保存時,將返回表單,其中兩個字段都為空,標記為紅色並標記為“此字段是必需的”標簽:
這種行為很奇怪,因為我在不同的模型中實現了各種TinyMCE編輯器,這些編輯器完美地運行。 我應該澄清一點,我希望這兩個領域都是強制性的。 問題是輸入的文本被丟棄,表單返回時兩個字段都為空。 以下是所有相關代碼:
companycms /新聞/ models.py
from django.db import models
from tinymce import models as tinymce_models
class Article(models.Model):
headline = models.CharField(max_length=200)
content = tinymce_models.HTMLField()
about = tinymce_models.HTMLField()
pub_date = models.DateTimeField('date published')
url = models.CharField(max_length=200)
companycms /新聞/ forms.py
from django import forms
from django.db.models import get_model
from django.contrib.auth.models import User
from companycms.widgets import AdvancedEditor
from news.models import Article
from django.db import models
class ArticleModelAdminForm(forms.ModelForm):
headline = forms.CharField(max_length=200)
content = forms.CharField(widget=AdvancedEditor())
about = forms.CharField(widget=AdvancedEditor())
pub_date = models.DateTimeField('date published')
url = forms.CharField(max_length=200)
class Meta:
model = Article
companycms /新聞/ admin.py
from django.contrib import admin
from news.models import Article
from news.forms import ArticleModelAdminForm
class ArticleAdmin(admin.ModelAdmin):
list_display = ('headline', 'pub_date',)
form = ArticleModelAdminForm
admin.site.register(Article, ArticleAdmin)
companycms / companycms / widgets.py
from django import forms
from django.conf import settings
from django.utils.safestring import mark_safe
class AdvancedEditor(forms.Textarea):
class Media:
js = ('/static/tiny_mce/tiny_mce.js',)
def __init__(self, language=None, attrs=None):
self.language = language or settings.LANGUAGE_CODE[:2]
self.attrs = {'class': 'advancededitor'}
if attrs: self.attrs.update(attrs)
super(AdvancedEditor, self).__init__(attrs)
def render(self, name, value, attrs=None):
rendered = super(AdvancedEditor, self).render(name, value, attrs)
return rendered + mark_safe(u'''
<script type="text/javascript">
tinyMCE.init({
mode: "textareas",
theme: "advanced",
plugins: "advhr,table,emotions,media,insertdatetime,directionality",
theme_advanced_toolbar_align: "left",
theme_advanced_toolbar_location: "top",
theme_advanced_buttons1:"bold,italic,underline,strikethrough,sub,sup,separator,justifyleft,justifycenter,justifyright,justifyfull,separator,formatselect,fontselect,fontsizeselect,forecolor",
theme_advanced_buttons2:"bullist,numlist,outdent,indent,ltr,rtl,separator,link,unlink,anchor,image,separator,table,insertdate,inserttime,advhr,emotions,media,charmap,separator,undo,redo",
theme_advanced_buttons3_add:"forecolor,backcolor",
theme_advanced_font_sizes:"170%,10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px,32px,48px",
height: "350px",
width: "653px"
});
</script>''')
檢查了JavaScript控制台后,沒有返回任何錯誤,我檢查了其他管理頁面,發現此錯誤沒有出現在其他地方。
在此先感謝您的幫助。
您應該在companycms / news / models.py中添加一個blank=True
參數:
content = tinymce_models.HTMLField(blank=True)
是的,在Django settings.py中注釋#'mode':'textareas',一切都應該沒問題! ;)
TINYMCE_DEFAULT_CONFIG = {
#'mode': "textareas",
'theme': "advanced",
'plugins': '''pagebreak, style, layer, table, save, advhr, advimage, advlink,
emotions, iespell, inlinepopups, insertdatetime, preview, media,
searchreplace, print, contextmenu, paste, directionality,
fullscreen, noneditable, visualchars, nonbreaking, xhtmlxtras,
template, wordcount, advlist, autosave''',
'theme_advanced_buttons1': '''bold, italic, underline, strikethrough, |,
justifyleft, justifycenter, justifyright,
justifyfull, fontselect, fontsizeselect,
fullscreen, code''',
'theme_advanced_buttons2': '''bullist, numlist, |, outdent, indent,
blockquote, |, undo, redo, |, link, unlink, |,
forecolor, backcolor''',
'theme_advanced_buttons3': '''tablecontrols, |, hr, sub, sup, |, charmap''',
'theme_advanced_toolbar_location': "top",
'theme_advanced_toolbar_align': "left",
'theme_advanced_statusbar_location': "bottom",
'theme_advanced_resizing': "true",
'template_external_list_url': "lists/template_list.js",
'external_link_list_url': "lists/link_list.js",
'external_image_list_url': "lists/image_list.js",
'media_external_list_url': "lists/media_list.js",
'style_formats': [
{'title': 'Bold text', 'inline': 'strong'},
{'title': 'Red text', 'inline': 'span', 'styles': {'color' : '#ff0000'}},
{'title': 'Help', 'inline': 'strong', 'classes': 'help'},
{'title': 'Table styles'},
{'title': 'Table row 1', 'selector': 'tr', 'classes': 'tablerow'}
],
'width': '700',
'height': '400'
}
我猜您的自定義表單和自定義小部件會給您帶來麻煩。 前兩件事。 只是為了確定...你在settings.py中添加了tinymce嗎?
INSTALLED_APPS = (
...
'tinymce',
)
在urlpatterns?
urlpatterns = patterns('',
...
(r'^tinymce/', include('tinymce.urls')),
)
根據文檔,您需要的是tinymce_models.HTMLField()。 像你一樣。 加載TinyMCE不需要所有其余代碼(自定義窗體和自定義窗口小部件)。 所以在admin.py中注釋掉:
#form = ArticleModelAdminForm
現在手指交叉並測試! 工作對嗎? 你可以重新打開它。
ArticleModelAdminForm只需要您要調整的字段。 刪除標題,pub_date和url字段。
不要在窗口小部件中添加js。 但是創建一個新的js文件。 刪除渲染功能。 添加js位置:
class Media:
js = ('/static/tiny_mce/tiny_mce.js',
'/static/tiny_mce/my_advanced_editor.js')
將類Media移動到ModelAdmin。 它裝載一次,而不是每個textarea。
希望能幫助到你!
編輯:
TinyMCE丟失了有關提交表單的數據,因為它已多次初始化。 Django沒有收到POST數據並正確顯示“此字段是必需的”。 所以一定要初始化TinyMCE一次:
models.py
class Article(models.Model):
content = models.TextField() # Normal textfields (Don't load Tiny)
about = models.TextField()
admin.py
class ArticleAdmin(admin.ModelAdmin):
class Media:
js = ('/static/tiny_mce/tiny_mce.js',
'/path/to/my_advanced_editor.js') # Add js to head of admin.
my_advanced_editor.js
tinyMCE.init({
mode: "textareas", // This applies Tiny to all textareas.
theme: "advanced",
...
});
獎勵:Django-TinyMCE使得將TinyMCE應用於字段變得“容易”,但選擇帶有TinyMCE的字段也很容易。 模式准確:
mode : "exact",
elements : "id_content,id_about",
或取消選擇:
mode: "textareas", // All textareas except...
editor_deselector : "NoEditor" // deselects class="NoEditor"
在最后一種情況下,您的FormField需要一個小部件:
widget=forms.TextInput(attrs={'class':'NoEditor'})
目前我遇到了同樣的問題。 我的解決方案是:
刪除行mode: "textareas",
現在提交后形成保存。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.