3

I am building a CMS in Mezzanine, and all in all I am extremely impressed with the system. I can't tell you how many CMS systems I have attempted to customize in the past and given up in total frustration.

All in all this has been smooth as silk. But I have a custom page for displaying a YouTube video, and the custom field (which is definitely present in the instance, according to the shell) is failing to render in the template.

The app is called mezz_youtube; here is models.py:

from django.db import models
from django.utils.translation import ugettext_lazy as _
from mezzanine.pages.models import Page, RichText


class YouTubePage(Page):
    """
    Implements pages with a YouTube video.
    """
    video_slug = models.SlugField(u"video ID", max_length=11)

    class Meta:
        verbose_name = _("YouTube page")
        verbose_name_plural = _("YouTube pages")

The Admin works fine and the YouTubePage was created successfully, as the shell shows:

In [1]: from mezz_youtube.models import YouTubePage

In [2]: p = YouTubePage.objects.get()

In [3]: p.pk
Out[3]: 11

In [4]: p.video_slug
Out[4]: u'CKqGbpR4vvM'

But in the template, as shown here with various debugging entries, the custom field does not appear. Here's the template:

{% extends "pages/page.html" %}

{% load mezzanine_tags %}

{% block main %}
<iframe width="853" height="480" src="http://www.youtube.com/embed/{{ page.video_slug }}" frameborder="0" allowfullscreen></iframe>

<p>Video slug is {% if page.video_slug %}present{% else %}absent{% endif %}.</p>
<p>Title: {{ page.title }} </p>
<p>Model: {{ page.content_model }} </p>
<p>fields:</p>
<ul>
{% for field, val in page.fields.iteritems %}
<li>{{ field }}: {{ val }}</li>
{% endfor %}
</ul>

{{ block.super }}

{% endblock %}

Here is the corresponding section of the output:

<iframe width="853" height="480" src="http://www.youtube.com/embed/" frameborder="0" allowfullscreen></iframe>
<p>Video slug is absent.</p>
<p>Title: ladeda </p>
<p>Model: youtubepage </p>
<p>fields:</p>
<ul>

</ul>

As you can see, the title field renders, but the video_slug field does not. Any idea what is going on here?

  • 4
    AHA! I figured it out. To use custom fields in Mezzanine templates, you need to interpose the custom model name in the variable. In this case, `{{ page.video_slug }}` does not resolve, but since `video_slug` is defined in the class `youtubepage` the variable *will* appear using `{{ page.youtubepage.video_slug }}`. – StarckTruth Jun 10 '13 at 20:30
  • I found the same in my Mezzanine app. Thanks for documenting the discovery process! – orlenko Jun 16 '13 at 07:24

0 Answers0