1

I'm following the Wagtail documentation on Snippets to make sure I can get that working before creating my own. But have come across a stumbling block. I have added the model for the adverts in the models.py file and am now creating the demo_tags.py file. Currently it reads -

from django import template
from demo.models import *

register = template.Library()

... #don't know if something is supposed to go in here

# Advert snippets
@register.inclusion_tag('demo/tags/adverts.html', takes_context=True)
def adverts(context):
    return {
        'adverts': Advert.objects.all(),
        'request': context['request'],
    }

When I run the development server the from demo.models import * line creates a InvalidTemplateLibraryerror.

Clearly I'm supposed to change the replace the 'demo' and '*' with something, but what?

Additionally, when it comes to creating the template, called adverts.html, which directory should that be going in? the templatetags one, or with the other blog templates?

Thanks.

1 Answers1

0

demo.models refers to the module where the Advert model is defined on the Wagtail demo website. If you've defined this somewhere else - such as the home/models.py file that's created on a new Wagtail project - you need to change this accordingly:

from home.models import *

You should crete the adverts.html at the location templates/demo/tags/adverts.html within your app directory. More generally - Django will look for a template at the path you've given ('demo/tags/adverts.html') relative to all valid template locations in your project - which typically means the 'templates' directories of all apps that exist in your project.

gasman
  • 23,691
  • 1
  • 38
  • 56
  • I changed it to `from blog.models import Advert, BlogIndexPage, BlockPage` as that's where the `Advert` is defined and there's no more error message :) – Josh Woodcock Mar 06 '17 at 13:18
  • Now I seem to have everything order. I've created the model and template, got the view working and added the {% adverts %} to the `blog_page` template BUT I can't see/edit the Snippet when I run the development server and edit a blog page. Am I missing something? – Josh Woodcock Mar 06 '17 at 13:32
  • I don't understand what you mean - but it sounds like that's separate to this question. Please can you open a new question, and explain what you're trying to do in more detail? – gasman Mar 06 '17 at 15:23
  • i got it sorted. I didn't realise that to make a snippet it's on the side of the admin just above `Settings` – Josh Woodcock Mar 07 '17 at 10:22