app to plugin django cms error

I want to create a plugin on a web page through an app, however, when loading the plugin to the page doesn't load the content of it, but on the page of the app if it loads the content of the application. I think that the problem can be in the code of the definition of the plugin or in the template, I tried with the suggestion in this link http://docs.django-cms.org/en/latest/how_to/custom_plugins.html#handling-relations, but it doesn't work just runs an error.

the app is :https://github.com/tomwalker/django_quiz/tree/master/quiz

I've been using 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

this is the code of 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

this is the code of 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

and this is a template /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 %}

Upvotes: 1

Views: 194

Answers (1)

markwalker_
markwalker_

Reputation: 12869

By default a plugin is passed to in the context as instance which you'll see there in the render method. However I think you'll have a problem because your template currently does {% for quiz in quiz_list %} but your plugin links to a single quiz.

Based on this you'd want to update your template to check against the instance;

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

If you've got a page which is showing all your quizzes though, you might be better looking into developing your application with an app hook so that you can attach an app to a CMS page, then your app would function as any usual django app. The docs on this are here; http://docs.django-cms.org/en/latest/how_to/apphooks.html

Upvotes: 0

Related Questions