I created a site with djangoCMS and am actively using apphooks, cms plugins, wizards, etc. We have a simple application with one model containing the basic data that should be displayed on the main page.
models.py
from django.db import models
from django.utils.text import slugify
from django.urls import reverse
from cms.models.fields import PlaceholderField
from djangocms_text_ckeditor.fields import HTMLField
class Programme(models.Model):
name = models.CharField(max_length=60, unique=True)
slug = models.SlugField()
icon = models.CharField(max_length=50, unique=True)
introduction = HTMLField()
overview = PlaceholderField(
'programme_overview',
related_name='programmes_overview'
)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse(
'programmes:programme-detail',
kwargs={'slug': self.slug}
)
def save(self, *args, **kwargs):
if not self.pk:
self.slug = slugify(self.name)
super(Programme, self).save(*args, **kwargs)
I decided to create a special templatetag template for this purpose.
templatetags/programmes_tags
from django import template
from ..models import Programme
register = template.Library()
@register.inclusion_tag('programmes/programme_list.html')
def programme_list():
programmes = Programme.objects.all()
return {'programmes': programmes}
In the template, I use render_model
out cms_tags
because editors should be able to edit content in the interface. Here is the template:
templates/programmes/programme_list.html
{% load cms_tags %}
{% for programme in programmes %}
<div class="col-lg-2 col-md-4 col-sm-6 col-xs-12 text-center flex-item">
<div class="service-box">
<i class="fa fa-4x {{ programme.icon }} text-primary" style="visibility:visible;"></i>
<h3>
<a href="{% url 'programmes:programme-detail' programme.slug %}">
{{ programme.name }}
</a>
</h3>
<p class="text-muted">
{% render_model programme 'introduction' %}
</p>
</div>
</div>
{% endfor %}
The tag is now used in the template for the main page:
{% load programmes_tags %}
{% programme_list %}
When I open the homepage, it gives an error message:
KeyError: 'request'
Obviously, the tag render_model
needs access to request
. When I try to change templatetag as follows:
@register.inclusion_tag('programmes/programme_list.html', takes_context=True)
def programme_list(context):
programmes = Programme.objects.all()
context.update({'programmes': programmes})
return context
request
RequestContext, :
ValueError: dictionary update sequence element
RequestContext?
templatetag - , , .