1

I'm trying to style a field in my form using django-tinymce without success, as it just displays an ordinary CharField in the form. The test form is part of a formset rendered with Django crispy forms.

models.py

class test(models.Model)
  test = models.CharField(max_length=100)

forms.py

from tinymce.widgets import TinyMCE


class TestForm(forms.ModelForm):


  class Meta:
    test = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
    model = test

  def __init__(self, *args, **kwargs):
    super(TestForm, self).__init__(*args, **kwargs)
    self.helper = FormHelper()
    self.helper.form_tag = False
    self.helper.layout = layout.Layout(
        layout.Fieldset(
            _("Test"),
            layout.Field("test"),
            ),
      )

urls.py

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'tripller.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^tinymce/', include('tinymce.urls')),
)

template

{% load i18n crispy_forms_tags %}
        <form method="post" action="." enctype="multipart/form-data">
          {% csrf_token %}
          {% crispy form %}
          {{ formset.management_form }}
                {{ formset.non_form_errors }}
                {% for form in formset %}
                    {{ form.id }}
                    <div class="inline {{ formset.prefix }}">   
                        {% crispy form %}
                        {{ form.description.errors }}
                        {{ form.description.label_tag }}
                        {{ form.description }}
                    </div>
                {% endfor %}
         </form>

settings.py

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ajaxuploader',
    'crispy_forms',
    'tinymce',
)

TINYMCE_JS_URL = os.path.join(STATIC_ROOT, "js/tiny_mce/tiny_mce.js")
TINYMCE_JS_ROOT = os.path.join(STATIC_ROOT, "js/tiny_mce")
TINYMCE_SPELLCHECKER = True
TINYMCE_COMPRESSOR = True
TINYMCE_DEFAULT_CONFIG = {
    'theme': "advanced",
    'plugins': "spellchecker",
    'theme_advanced_buttons3_add': "|,spellchecker",
}
  1. Is this the correct way of specifying the static files url, whereby all my js files are stored in the static folder instead of the media folder as suggested in the docs? Is the CharField not working due to the parameters defined incorrectly?

  2. How can I specify a Textfield displayed using TinyMCE editor?

  3. Assuming that a user finishes editing the form via TinyMCE, how can I display the submitted data as a html form?

zan
  • 594
  • 2
  • 8
  • 20

2 Answers2

2

Try to put above form {{ form.media }}

{% load i18n crispy_forms_tags %}
        {{ form.media }}
        <form method="post" action="." enctype="multipart/form-data">
          {% csrf_token %}
          {% crispy form %}
          {{ formset.management_form }}
                {{ formset.non_form_errors }}
                {% for form in formset %}
                    {{ form.id }}
                    <div class="inline {{ formset.prefix }}">   
                        {% crispy form %}
                        {{ form.description.errors }}
                        {{ form.description.label_tag }}
                        {{ form.description }}
                    </div>
                {% endfor %}
         </form>
Milovan Tomašević
  • 6,823
  • 1
  • 50
  • 42
1

Field definition should be on TestForm level, not in the TestForm.Meta:

class TestForm(forms.ModelForm):

    test = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))

    class Meta:
        model = test
catavaran
  • 44,703
  • 8
  • 98
  • 85
  • Oops... My bad.. It's still showing same form but the test field is shortened, based on the attrs specified in test. The editor is not displayed :-( – zan Jan 18 '15 at 12:17