应用程序到插件 django cms 错误

Posted

技术标签:

【中文标题】应用程序到插件 django cms 错误【英文标题】:app to plugin django cms error 【发布时间】:2018-09-19 13:10:02 【问题描述】:

我想通过一个应用程序在网页上创建一个插件,但是,当将插件加载到页面时不会加载它的内容,而是在应用程序的页面上加载应用程序的内容.我认为问题可能出在插件定义的代码或模板中,我尝试了此链接中的建议http://docs.django-cms.org/en/latest/how_to/custom_plugins.html#handling-relations,但它不起作用只是运行错误。

应用程序是:https://github.com/tomwalker/django_quiz/tree/master/quiz

我一直在使用 python 3.4、django 1.8、djangoCMS 3.5

this is how the plugin content is displayed

This is how it should look, this is the content of the application

这是models.py的代码

from django.db import models
from cms.models import CMSPlugin
from quiz.models import Quiz, Question


class QuizPluginModel(CMSPlugin):
    quiz = models.ForeignKey(Quiz)

    def __unicode__(self):
        return self.quiz.question

这是cms_plugins.py的代码

from cms.plugin_base import CMSPluginBase
from cms.plugin_pool import plugin_pool
from quiz_cms_integration.models import QuizPluginModel, QuestionPluginModel
from django.utils.translation import gettext as _


@plugin_pool.register_plugin  # register the plugin
class QuizPluginPublisher(CMSPluginBase):
    model = QuizPluginModel  # model where plugin data are saved
    module = _("Quiz")
    name = _("Quiz Plugin")  # name of the plugin in the interface
    render_template = "quiz_cms_integrations/quiz_list.html"

    def render(self, context, instance, placeholder):
        context.update('instance': instance)
        return context

这是一个模板 /quiz_list.html

% extends 'base_q.html' %
% load i18n %
% block title %% trans "All Quizzes" %% endblock %

% block content %
<h2>% trans "List of quizzes" %</h2>
    % if quiz_list %
        <table class="table table-bordered table-striped">

          <thead>
            <tr>
              <th>% trans "Title" %</th>
              <th>% trans "Category" %</th>
              <th>% trans "Exam" %</th>
              <th>% trans "Single attempt" %</th>
              <th></th>
            </tr>
          </thead>

          <tbody>

         % for quiz in quiz_list %

             <tr>
               <td> quiz.title </td>
               <td> quiz.category </td>
              <td> quiz.exam_paper </td>
               <td> quiz.single_attempt </td>
               <td>
                <a href="% url 'quiz_start_page' slug=quiz.url %">
                  % trans "View details" %
                </a>
            </tr>

        % endfor %
          </tbody>

        </table>

    % else %
        <p>% trans "There are no available quizzes" %.</p>
    % endif %
% endblock %

【问题讨论】:

【参考方案1】:

默认情况下,插件在上下文中以instance 的形式传递给您,您将在render 方法中看到它。但是我认为您会遇到问题,因为您的模板当前执行% for quiz in quiz_list %,但您的插件链接到单个测验。

基于此,您希望更新模板以检查实例;

% extends 'base_q.html' %
% load i18n %
% block title %% trans "All Quizzes" %% endblock %

% block content %
<h2>% trans "List of quizzes" %</h2>
    % if instance %
        <table class="table table-bordered table-striped">

          <thead>
            <tr>
              <th>% trans "Title" %</th>
              <th>% trans "Category" %</th>
              <th>% trans "Exam" %</th>
              <th>% trans "Single attempt" %</th>
              <th></th>
            </tr>
          </thead>

          <tbody>

             <tr>
               <td> instance.quiz.title </td>
               <td> instance.quiz.category </td>
               <td> instance.quiz.exam_paper </td>
               <td> instance.quiz.single_attempt </td>
               <td>
                <a href="% url 'quiz_start_page' slug=instance.quiz.url %">
                  % trans "View details" %
                </a>
            </tr>
          </tbody>

        </table>

    % else %
        <p>% trans "There are no available quizzes" %.</p>
    % endif %
% endblock %

如果您有一个显示所有测验的页面,您可能会更好地考虑使用应用挂钩开发您的应用程序,以便您可以将应用程序附加到 CMS 页面,那么您的应用程序将像任何应用程序一样运行常用的 django 应用程序。这方面的文档在这里; http://docs.django-cms.org/en/latest/how_to/apphooks.html

【讨论】:

解决方案奏效了,谢谢,我把应用程序作为apphook,效果很好,我想这就是plugin和apphook应用程序复杂性的区别。 @MarcoAntonioDíazVillarreal 没错。如果你有一个有过程的东西,你可能想把它变成一个应用程序。如果它更简单、更可重用,您通常希望将其制成插件。

以上是关于应用程序到插件 django cms 错误的主要内容,如果未能解决你的问题,请参考以下文章

渲染模板 django cms 插件中的简单循环

django django-cms 我没有成功访问应用程序的 urls.py

Django CMS 用户无权添加插件

Django-CMS 自定义插件未在已发布页面中显示数据

django cms [aldryn newsblog] 替换插件模板

django 可重用应用程序:删除迁移中的依赖项?