2013-07-17 62 views
1

我目前正在使用Django框架將內容管理系統構建到公司網站中,並且在成功完成一個頁面後,開始在需要豐富的不同頁面上工作文本編輯器在管理界面。Django TinyMCE:'ModelFormMetaclass'對象不可迭代

選擇了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似乎正確安裝,並且問題在於模型。模型

我的目標是,如果不清楚,是創建一個帶有一個內容字段的簡單MyModel模型,可以使用TinyMCE編輯器進行編輯。如果您認爲我可以提供更多代碼以更好地瞭解情況,請告訴我。謝謝。

+0

如果你需要TinyMCE用於Django管理檢查這個要點https://gist.github.com/bofh19/6021510這是一個簡單的hack的base_site.html和使Django的管理員使用tinyMCE在所有textarea字段 – boltsfrombluesky

+0

嗨,感謝您的幫助。當我嘗試使用您的代碼時,我收到了一個GET 404錯誤,表示無法找到主題,現在我收到與我自己本地版本的腳本相同的錯誤: GET http://.../ company/admin/contact/column/1/themes/modern/theme.js/404(NOT FOUND)tiny_mce.js:7551 無法加載:http://.../company/admin/contact/column/1 //themes/modern/theme.js –

+0

嗨..我還沒有在models.py或admin.py中使用任何編輯...只是在admin/base_site.html中創建了一個文件,並在gist中添加了內容..這裏是我的網站http://backpaper.w4rlock.in/media/image/ss.png的屏幕截圖 – boltsfrombluesky

回答

0

我覺得我有點用你的模型定義感到困惑,無論哪種方式,我認爲你的錯誤是在這一行

admin.site.register(MyModelForm, MyModelAdmin) 

除非我被名字MyModelForm你傳遞的ModelForm來上當以models.Model子類作爲參數的註冊方法。

0

這真的是晚了派對,但這是你需要的。

admin.site.register(MyModel, MyModelAdmin)

MyModelAdminMyModelFormadmin.site.register需要ModelModelAdmin