2

我目前正在使用 Django 框架将内容管理系统构建到公司网站中,并在成功完成一个页面后,开始在另一个页面上工作,该页面需要在管理界面中使用富文本编辑器。

为这项工作选择了 TinyMCE,周一我花了很多时间试图让它正确运行,但只设法包含一个简单的 HTMLField()。被许多不同的教程稍微弄糊涂了,我现在遇到了一个我正在努力寻找解决方案的错误,我希望 StackOverflow 社区可以提供帮助。

目前,我有以下代码:

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)

出于调试目的,此代码的大部分已被注释掉,因为问题似乎出在 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

这是我收到的回溯:

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

如您所见,tinymce 似乎已正确安装,问题出在 models.ModelForm

如果不清楚,我的目标是创建一个简单的 MyModel 模型,其中包含一个内容字段,可使用 TinyMCE 编辑器进行编辑。如果您认为我可以提供更多代码来阐明这种情况,请告诉我。谢谢。

4

2 回答 2

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.

于 2013-07-21T19:07:00.943 回答
0

这真的很晚了,但这是你需要的。

admin.site.register(MyModel, MyModelAdmin)

MyModelAdmin并且需要 aMyModelForm和aadmin.site.registerModelModelAdmin

于 2016-03-31T22:51:59.850 回答