将 Django Flatpages 与 TinyMCE 结合使用
Use Django Flatpages with TinyMCE
我正在尝试使用 Django Flatpages(进入管理)实现 Django TinyMCE 小部件。
我已经读过这个Embedding tinyMCE in django flatpage
我做了 sabe,但没有用。这是我的 admin.py 代码:
from django.contrib.flatpages.admin import FlatpageForm, FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from tinymce.widgets import TinyMCE
from django.contrib import admin
class PageForm(FlatpageForm):
class Meta:
model = FlatPage
widgets = {
'content': TinyMCE(attrs={'cols': 100, 'rows': 15}),
}
class PageAdmin(FlatPageAdmin):
"""
Page Admin
"""
form = PageForm
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, PageAdmin)
我的站点 运行 很好,没有错误,可以编辑或创建新的 Django Flatpage,但小部件没有出现。有任何想法吗?
我做了一些不同的事情,我在 meta Class 中遗漏了 "fields" 属性,按照新代码,测试并 运行:
from django.contrib.flatpages.admin import FlatpageForm, FlatPageAdmin
from django.contrib import admin
from django import forms
from django.contrib.flatpages.models import FlatPage
from tinymce.widgets import TinyMCE
class FlatPageForm(forms.ModelForm):
content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
class Meta:
model = FlatPage
fields = '__all__'
class PageAdmin(FlatPageAdmin):
"""
Page Admin
"""
form = FlatPageForm
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, PageAdmin)
接受的答案对我不起作用(Django 1.10)所以这对我有用。
我首先在我的项目中创建了一个名为 content 的新应用来保持条理。
manage.py startapp content
我已将此添加到我的 admin.py
from django.contrib import admin
from django.contrib.flatpages.admin import FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from django.db import models
from tinymce.widgets import TinyMCE
# Register your models here.
class PageAdmin(FlatPageAdmin):
"""
Page Admin
"""
formfield_overrides = {
models.TextField: {'widget': TinyMCE(attrs={'cols': 100, 'rows': 15})},
}
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, PageAdmin)
最后 运行 manage.py collectstatic
,重启你的网络服务器并刷新。
我正在尝试使用 Django Flatpages(进入管理)实现 Django TinyMCE 小部件。
我已经读过这个Embedding tinyMCE in django flatpage
我做了 sabe,但没有用。这是我的 admin.py 代码:
from django.contrib.flatpages.admin import FlatpageForm, FlatPageAdmin
from django.contrib.flatpages.models import FlatPage
from tinymce.widgets import TinyMCE
from django.contrib import admin
class PageForm(FlatpageForm):
class Meta:
model = FlatPage
widgets = {
'content': TinyMCE(attrs={'cols': 100, 'rows': 15}),
}
class PageAdmin(FlatPageAdmin):
"""
Page Admin
"""
form = PageForm
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, PageAdmin)
我的站点 运行 很好,没有错误,可以编辑或创建新的 Django Flatpage,但小部件没有出现。有任何想法吗?
我做了一些不同的事情,我在 meta Class 中遗漏了 "fields" 属性,按照新代码,测试并 运行:
from django.contrib.flatpages.admin import FlatpageForm, FlatPageAdmin
from django.contrib import admin
from django import forms
from django.contrib.flatpages.models import FlatPage
from tinymce.widgets import TinyMCE
class FlatPageForm(forms.ModelForm):
content = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
class Meta:
model = FlatPage
fields = '__all__'
class PageAdmin(FlatPageAdmin):
"""
Page Admin
"""
form = FlatPageForm
admin.site.unregister(FlatPage)
admin.site.register(FlatPage, PageAdmin)
接受的答案对我不起作用(Django 1.10)所以这对我有用。
我首先在我的项目中创建了一个名为 content 的新应用来保持条理。
manage.py startapp content
我已将此添加到我的 admin.py
from django.contrib import admin from django.contrib.flatpages.admin import FlatPageAdmin from django.contrib.flatpages.models import FlatPage from django.db import models from tinymce.widgets import TinyMCE # Register your models here. class PageAdmin(FlatPageAdmin): """ Page Admin """ formfield_overrides = { models.TextField: {'widget': TinyMCE(attrs={'cols': 100, 'rows': 15})}, } admin.site.unregister(FlatPage) admin.site.register(FlatPage, PageAdmin)
最后 运行
manage.py collectstatic
,重启你的网络服务器并刷新。