I have a django project with 'news' applications. here is the model:
我有一个django项目,它有“新闻”应用程序。这是模型:
class News(models.Model):
title = CharField(max_length=255)
content = TextField()
How can I add django-cms to this project to be able modify news directly in template? I know I need a custom plugin based on 'news' model, and here I have two choices: 1) I can describe all fields of 'news' model in single plugin, but then there will be a window to edit all fields at once.
如何将django-cms添加到这个项目中,以便能够在模板中直接修改新闻?我知道我需要一个基于“新闻”模型的自定义插件,这里我有两个选择:1)我可以在一个插件中描述“新闻”模型的所有字段,然后会有一个窗口同时编辑所有字段。
plugin, that describes all fields:
插件,描述所有字段:
class NewsPlugin(CMSPlugin):
title = CharField(max_length=255)
content = TextField()
def __unicode__(self):
return self.title
2) The second idea is about to write plugins for each separate field in 'news' and combine them in some another plugin, but I dont know how to realize it.
第二个想法是为“新闻”中的每个独立字段编写插件,并将它们合并到另一个插件中,但我不知道如何实现它。
separate plugin for news title:
新闻标题独立插件:
class NewsTitle(CMSPlugin):
title = CharField(max_length=255)
separate pluginn for news content:
新闻内容的独立插件:
class NewsContent(CMSPlugin):
content = TextField()
Any idea?
任何想法?
2 个解决方案
#1
1
What you might want to consider is an alternative to plugins which I find works very well with a news application.
您可能想要考虑的是一个替代插件,我发现它在新闻应用程序中工作得很好。
You could change your news content field to a PlaceholderField
which will allow you to add plugin's to a news item and also configure the placeholder config with all the usual options available to standard template placeholders.
您可以将新闻内容字段更改为PlaceholderField,它允许您将插件的内容添加到新闻条目中,还可以配置占位符配置,并使用标准模板占位符提供的所有常用选项。
In my news app I have a fairly typical setup, a ListView
and then a DetailView
where you can switch the CMS to edit mode & edit the plugins in your news item's PlaceholderField
.
在我的新闻应用中,我有一个相当典型的设置,一个ListView,然后一个DetailView,你可以将CMS切换到编辑模式和编辑你新闻项目的PlaceholderField中的插件。
You could also extend the toolbar to offer links to add new news items in a modal dialog, or list the existing news items;
您还可以扩展工具栏,以提供在模态对话框中添加新新闻项的链接,或列出现有新闻项;
@toolbar_pool.register
class LatestNewsToolbar(CMSToolbar):
def populate(self):
news_menu = self.toolbar.get_or_create_menu(
NEWS_MENU_IDENTIFIER, NEWS_MENU_NAME
)
position = news_menu.get_alphabetical_insert_position(
_('Latest news'),
SubMenu
)
menu = news_menu.get_or_create_menu(
'latest_news_menu',
_('Latest News ...'),
position=position
)
try:
menu.add_modal_item(
_('Add News Item'),
url=admin_reverse('news_latestnews_add')
)
except NoReverseMatch:
# not in urls
pass
try:
menu.add_modal_item(
_('Existing News Items'),
url=admin_reverse('news_latestnews_changelist')
)
except NoReverseMatch:
# not in urls
pass
def post_template_populate(self):
pass
def request_hook(self):
pass
If you have a play with this way of working, I think you'll find it more suitable & more powerful than plugins :)
如果您使用这种工作方式,我认为您会发现它比插件更合适、更强大:)
And checkout this video on this area of CMS; https://www.youtube.com/watch?time_continue=2670&v=Dj8dhgmzlFM
这段视频是关于CMS的;https://www.youtube.com/watch?time_continue=2670&v=Dj8dhgmzlFM
#2
0
Use plugins to deliver news articles from your application into other django CMS content - don't use plugins to create news content.
使用插件将来自应用程序的新闻文章传递到其他django CMS内容中——不要使用插件来创建新闻内容。
See the django CMS documentation on how to use placeholders in other applications.
参见django CMS文档,了解如何在其他应用程序中使用占位符。
#1
1
What you might want to consider is an alternative to plugins which I find works very well with a news application.
您可能想要考虑的是一个替代插件,我发现它在新闻应用程序中工作得很好。
You could change your news content field to a PlaceholderField
which will allow you to add plugin's to a news item and also configure the placeholder config with all the usual options available to standard template placeholders.
您可以将新闻内容字段更改为PlaceholderField,它允许您将插件的内容添加到新闻条目中,还可以配置占位符配置,并使用标准模板占位符提供的所有常用选项。
In my news app I have a fairly typical setup, a ListView
and then a DetailView
where you can switch the CMS to edit mode & edit the plugins in your news item's PlaceholderField
.
在我的新闻应用中,我有一个相当典型的设置,一个ListView,然后一个DetailView,你可以将CMS切换到编辑模式和编辑你新闻项目的PlaceholderField中的插件。
You could also extend the toolbar to offer links to add new news items in a modal dialog, or list the existing news items;
您还可以扩展工具栏,以提供在模态对话框中添加新新闻项的链接,或列出现有新闻项;
@toolbar_pool.register
class LatestNewsToolbar(CMSToolbar):
def populate(self):
news_menu = self.toolbar.get_or_create_menu(
NEWS_MENU_IDENTIFIER, NEWS_MENU_NAME
)
position = news_menu.get_alphabetical_insert_position(
_('Latest news'),
SubMenu
)
menu = news_menu.get_or_create_menu(
'latest_news_menu',
_('Latest News ...'),
position=position
)
try:
menu.add_modal_item(
_('Add News Item'),
url=admin_reverse('news_latestnews_add')
)
except NoReverseMatch:
# not in urls
pass
try:
menu.add_modal_item(
_('Existing News Items'),
url=admin_reverse('news_latestnews_changelist')
)
except NoReverseMatch:
# not in urls
pass
def post_template_populate(self):
pass
def request_hook(self):
pass
If you have a play with this way of working, I think you'll find it more suitable & more powerful than plugins :)
如果您使用这种工作方式,我认为您会发现它比插件更合适、更强大:)
And checkout this video on this area of CMS; https://www.youtube.com/watch?time_continue=2670&v=Dj8dhgmzlFM
这段视频是关于CMS的;https://www.youtube.com/watch?time_continue=2670&v=Dj8dhgmzlFM
#2
0
Use plugins to deliver news articles from your application into other django CMS content - don't use plugins to create news content.
使用插件将来自应用程序的新闻文章传递到其他django CMS内容中——不要使用插件来创建新闻内容。
See the django CMS documentation on how to use placeholders in other applications.
参见django CMS文档,了解如何在其他应用程序中使用占位符。