2

I'm currently working with the Django framework to build a content management system into a company website, and after successfully completing one page, began work on a different page that requires a rich text editor within the admin interface.

Having chosen TinyMCE for the job, I spent a lot of time on Monday trying to get it running correctly, but only managed to include a simple HTMLField(). Having been slightly confused by a number of differing tutorials, I've now encountered an error that I'm struggling to find a solution to, and one which I hoped the StackOverflow community might be able to help with.

Currently, I have the following code:

admin.py

  from django.forms import *
  from django.db.models import *
  from tinymce.widgets import TinyMCE
  from models import *
  from django.contrib import admin

  #class MyModelForm(forms.ModelForm):
      #content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 10}))

      #class Meta:
          #model = MyModel

  class MyModelAdmin(admin.ModelAdmin):
      form = MyModelForm

  admin.site.register(MyModelForm, MyModelAdmin)

The majority of this code has been commented out for debugging purposes, as the problem seems to be with models.py:

models.py

  from django import forms
  from django.contrib.flatpages.models import FlatPage
  from tinymce.widgets import TinyMCE

  class MyModelForm(forms.ModelForm):
      content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows' : 30}))

       class Meta:
           model = FlatPage

This is the traceback I'm being presented with:

Environment:


Request Method: GET
Request URL: mycomputer.mydomain/company/admin/

Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'cms',
 'mptt',
 'menus',
 'south',
 'sekizai',
 'cms.plugins.file',
 'cms.plugins.link',
 'cms.plugins.picture',
 'cms.plugins.text',
 'reversion',
 'grappelli',
 'profiles',
 'tinymce',
 'testapp',
 'django_reset')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'cms.middleware.page.CurrentPageMiddleware',
 'cms.middleware.user.CurrentUserMiddleware',
 'cms.middleware.toolbar.ToolbarMiddleware')


Traceback:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  103.                     resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in resolve
  319.             for pattern in self.url_patterns:
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in url_patterns
  347.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python2.7/dist-packages/django/core/urlresolvers.py" in urlconf_module
  342.             self._urlconf_module = import_module(self.urlconf_name)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/systems/vhost-trunk/djangocms/companycms/companycms/urls.py" in <module>
  7. admin.autodiscover()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/__init__.py" in autodiscover
  29.             import_module('%s.admin' % app)
File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py" in import_module
  35.     __import__(name)
File "/home/systems/vhost-trunk/djangocms/companycms/testapp/admin.py" in <module>
  17. admin.site.register(MyModelForm, MyModelAdmin)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in register
  77.         for model in model_or_iterable:

Exception Type: TypeError at /admin/
Exception Value: 'ModelFormMetaclass' object is not iterable

As you can see, tinymce appears to be correctly installed, and the problem lies with models.ModelForm

My aim, in case it isn't clear, is to create a simple MyModel model with one content field, editable using the TinyMCE Editor. If you think I could supply any more code to shed more light on the situation, please let me know. Thanks.

James McAulay
  • 63
  • 2
  • 10
  • if you need tinyMCE in django admin check this gist https://gist.github.com/bofh19/6021510 this is a simple hack of base_site.html and making django admin use tinyMCE at all textarea fields – boltsfrombluesky Jul 17 '13 at 15:16
  • Hi, thanks for your help. When I tried using your code, I received a GET 404 error saying themes couldn't be found, and now I'm receiving the same error with my own local version of the script: GET http://.../company/admin/contact/column/1/themes/modern/theme.js/ 404 (NOT FOUND) tiny_mce.js:7551 Failed to load: http://.../company/admin/contact/column/1//themes/modern/theme.js – James McAulay Jul 17 '13 at 16:05
  • hi.. i haven't used any edits in models.py or admin.py ... just created a file in admin/base_site.html and added the content in the gist.. here is the screen shot i have in my site http://backpaper.w4rlock.in/media/image/ss.png – boltsfrombluesky Jul 17 '13 at 17:48

2 Answers2

2

I think I'm a bit confused with your model definition, either way I think your error is in this line

admin.site.register(MyModelForm, MyModelAdmin)

unless I'm being fooled by the name MyModelForm you are passing a ModelForm to the register method which takes a models.Model subclass as an argument.

Paulo
  • 6,982
  • 7
  • 42
  • 56
0

This is really late to the party but this is what you need.

admin.site.register(MyModel, MyModelAdmin)

The MyModelAdmin has the MyModelForm and admin.site.register takes a Model and a ModelAdmin

Imposter
  • 253
  • 1
  • 10