python - TinyMCE with Django: "This field is required" -


i'm having issues using tinymce editor within django admin interface. when entering text 2 particular tinymce fields , pressing save, form returned both fields empty, flagged red , tagged "this field required" label:

enter image description here

this behaviour odd, have implemented various tinymce editors within different models have worked perfectly. should clarify wish both fields mandatory. problem text entered being discarded, , form returned both fields empty. here of relevant code:

companycms/news/models.py

from django.db import models tinymce import models  tinymce_models  class article(models.model):     headline = models.charfield(max_length=200)     content = tinymce_models.htmlfield()     = tinymce_models.htmlfield()     pub_date = models.datetimefield('date published')     url = models.charfield(max_length=200) 

companycms/news/forms.py

from django import forms django.db.models import get_model django.contrib.auth.models import user companycms.widgets import advancededitor news.models import article django.db import models  class articlemodeladminform(forms.modelform):     headline = forms.charfield(max_length=200)     content = forms.charfield(widget=advancededitor())     = forms.charfield(widget=advancededitor())     pub_date = models.datetimefield('date published')     url = forms.charfield(max_length=200)      class meta:          model = article 

companycms/news/admin.py

from django.contrib import admin news.models import article 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 django.conf import settings 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>''') 

having checked javascript console, there no errors being returned, , have checked other admin pages find error doesn't appear anywhere else.

thanks in advance help.

i guess custom form , custom widget give trouble. first 2 things. sure... did add tinymce in settings.py?

installed_apps = (     ...     'tinymce', ) 

and in urlpatterns?

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

according documentation need tinymce_models.htmlfield(). did. rest of code (custom form , custom widget) not necessary load tinymce. in admin.py comment out:

#form = articlemodeladminform 

now fingers crossed , test! works right? can switch on.

articlemodeladminform needs fields want adjust. remove headline, pub_date , url fields.

don't add js in widget. create new js file. delete render function. add js location:

    class media:         js = ('/static/tiny_mce/tiny_mce.js',                '/static/tiny_mce/my_advanced_editor.js') 

move class media modeladmin. it's loaded once, , not each textarea.

hope helps!

edit:

tinymce looses data on submitting form because initialized many times. django doesn't receive post data , correctly displays "this field required". make sure initialize tinymce once:

models.py

class article(models.model):     content = models.textfield() # normal textfields (don't load tiny)     = 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 head of admin. 

my_advanced_editor.js

   tinymce.init({         mode: "textareas",  // applies tiny textareas.         theme: "advanced",                   ...     }); 

bonus: django-tinymce makes 'easy' apply tinymce fields selecting fields tinymce quite easy. mode exact:

mode : "exact", elements : "id_content,id_about", 

or deselect:

    mode: "textareas",  // textareas except... editor_deselector : "noeditor" // deselects class="noeditor" 

in last case formfield needs widget:

    widget=forms.textinput(attrs={'class':'noeditor'}) 

Comments