应用程序插件django cms错误

时间:2018-04-10 03:15:39

标签: python django plugins django-templates django-cms

我想通过应用程序在网页上创建一个插件,但是,当将插件加载到页面时不加载它的内容,但是如果加载应用程序的内容则在应用程序的页面上加载。我认为问题可以在插件定义的代码中或模板中,我尝试使用此链接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 个答案:

答案 0 :(得分:0)

默认情况下,插件会在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