0
votes

I have created custom events plugin and now I want to display closest event on my home page (django-cms page with home.html template). How can I do it? This is my event/models.py

from django.core.urlresolvers import reverse
from django.db import models

from adminsortable.models import Sortable
from easy_thumbnails.fields import ThumbnailerImageField
from cms.models.fields import PlaceholderField
from cms.models.pluginmodel import CMSPlugin


class Event(Sortable):
    class Meta:
        app_label = 'event'


    event_name = models.CharField(
        blank=False,
        default='',
        max_length=64,
    )

    date = models.DateField(
        auto_now=False, 
        auto_now_add=False,
    )

    photo = ThumbnailerImageField(
        upload_to='events',
        blank=True,
        )

    def __unicode__(self):
        return self.event_name
1
I created views.py and then in template {{ event.name }}, but it doesnt work in cms template. - vegazz
Did you add an instance of the plugin to a placeholder? - Brandon
No, i found this django-cms.readthedocs.org/en/latest/extending_cms/… I need something like this, but I dont know how to do it correctly - vegazz
Plugins need to be added to placeholders in order to display them. When editing your page in the CMS in Django admin, add an instance of your plugin to one of the placeholders in your page, publish your changes, and it should show up. - Brandon
I know, but I dont now how to create plugin that shows only closest Event. I have allready created Event app - it shows all events on page /events. - vegazz

1 Answers

1
votes

To get the latest event, you can override the render method of your plugin:

class YourCMSPlugIn(CMSPluginBase):
    model = Event
    ...

    def render(self, context, instance, placeholder):
        context.update({
            'latest_event': self.model.objects.all()[:1],
            'placeholder': placeholder
        })

        return context

See: http://docs.django-cms.org/en/latest/extending_cms/custom_plugins.html#storing-configuration for more information.