Search code examples
pythondjangodjango-templatesdjango-cms

How to display django model data on the django cms page template


I would like to be able to use my external app data on django cms page. I am able to use custom plugin data but not data from normal django app

I tried creating views to handle my data but how do I call this view from django cms pages? here is exactly what I am asking for but his explanation is shallow and the link provided in the answer is no longer in use.

Here is my model:

class ExternalArticle(models.Model):
    url = models.URLField()
    source = models.CharField(
        max_length=100,
        help_text="Please supply the source of the article",
        verbose_name="source of the article",
    )
    title = models.CharField(
        max_length=250,
        help_text="Please supply the title of the article",
        verbose_name="title of the article",
    )
    class Meta:
        ordering = ["-original_publication_date"]

    def __str__(self):
        return u"%s:%s" % (self.source[0:60], self.title[0:60])

My template has placeholders

{% load cms_tags %}

{% block title %}{% page_attribute "page_title" %}{% endblock title %}

{% block content %}
    <section class="section">
        <div class="container">

             <div class="row">
                    <!-- header-->
                    <div class="col-lg-12">
                        <div class="updates">                           
                           {% placeholder "header" %}                         
                        </div>
                    </div>
                    <!-- header end-->

            </div> <!-- end row --> 

but I don't mind displaying this data anywhere on the template if not possible inside a place holder I have a custom page that I am using in Django cms. I would like to display the above data is a section in the Django cms page If this model was inheriting from CMSPlugin then that would be easy because I could use a custom plugin in my placeholder

I expect to display the data from my model in the template.


Solution

  • I was able to achieve this by doing the following:

    @plugin_pool.register_plugin
    class ArticlesPluginPublisher(CMSPluginBase):
        model = ArticlesPluginModel
        name = _("Articles")
        render_template = "article_plugin/articles.html"
        cache = False
    
        def render(self, context, instance, placeholder):
            context = super(ArticlesPluginPublisher, self).render(
                context, instance, placeholder
            )
            context.update(
                {
                    "articles": Article.objects.order_by(
                        "-original_publication_date"
                    )
                }
            )
            return context
    

    The plugin model(ArticlesPluginModel) is just for storing the configurations for the instance of the plugin. Not the actual articles. Then the render will just add to the context the relevant articles from the external app(Article)