【问题标题】:How to display django model data on the django cms page template如何在 django cms 页面模板上显示 django 模型数据
【发布时间】:2019-09-03 00:35:11
【问题描述】:

我希望能够在 django cms 页面上使用我的外部应用程序数据。 我可以使用自定义插件数据,但不能使用来自普通 django 应用程序的数据

我尝试创建视图来处理我的数据,但如何从 django cms 页面调用此视图? here 正是我所要求的,但他的解释很肤浅,答案中提供的链接不再使用。

这是我的模型:

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])

我的模板有占位符

{% 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 --> 

但如果在占位符内不可能,我不介意在模板上的任何位置显示此数据 我有一个在 Django cms 中使用的自定义页面。 我想显示上面的数据是Django cms页面中的一个部分 如果此模型继承自 CMSPlugin,那将很容易,因为我可以在占位符中使用自定义插件

我希望在模板中显示来自我的模型的数据。

【问题讨论】:

  • 您是否创建了插件、apphook 或上下文处理器以使您的模型可用于 CMS?
  • 我认为没有插件也可以实现,但如果没有插件就无法实现,那么我将创建一个插件。

标签: python django django-templates django-cms


【解决方案1】:

我能够通过执行以下操作来实现这一点:

@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

插件模型(ArticlesPluginModel) 仅用于存储插件实例的配置。不是实际的文章。 然后渲染只会将来自外部应用程序的相关文章添加到上下文中(Article)

【讨论】:

    【解决方案2】:

    您必须以某种方式将ExternalArticle 与页面对象连接起来。例如

    【讨论】:

      【解决方案3】:
        {% load cms_tags %}
      <h1>{{ instance.poll.question }}</h1>
      
      <form action="{% url polls.views.vote poll.id %}" method="post"> {% csrf_token %} 
              {% for choice in instance.poll.choice_set.all %}
                <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}" />
                <label for="choice{{ forloop.counter }}">{{ choice.choice }}</label><br /> 
               {% endfor %} 
      
                <input type="submit" value="Vote" /> 
      </form>
      

      【讨论】:

      • 我已经尝试过这个,但它不能在 Django cms 模板上工作。它适用于普通的 Django 模板
      • 确保你添加了这个 {% load cms_tags %}
      • 我刚刚用模板的 sn-p 编辑了我的问题。所以我想在 updates 类中的任何地方显示数据,无论是否可以使用占位符
      • 查看是否要在模板中显示数据需要在视图中传递上下文请添加视图sn-p
      • cms 的工作方式与 django 视图的工作方式不同。我希望你的答案是关于 django cms 而不仅仅是 django,因为你所说的一切在普通的 django 应用程序中都是有意义的,我们使用视图来连接模型和模板
      猜你喜欢
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2014-07-18
      • 2017-11-13
      • 2016-10-15
      • 2013-02-17
      • 1970-01-01
      • 2021-08-01
      相关资源
      最近更新 更多