Django Custom Inclusion Tags

后端 未结 4 521
太阳男子
太阳男子 2021-01-19 01:33

I\'m trying to build my own template tags. I have no idea why I getting these errors. I\'m following the Django doc\'s.

This is my app\'s file structure:

         


        
相关标签:
4条回答
  • 2021-01-19 02:07

    You forgot to close your template tags... Also, you should change the name in the for tag, you can't have for poll in poll:

    <ul>
    {% for p in poll %} <!--here-->
        <li>{{ p.pollquiz }}</li>
    {% endfor %} <!--and here-->
    </ul>
    

    Also notice you're not using the inclusion tag you defined at all. I think you got some code mixed up, try to go over a tutorial start to end and things will be clearer.

    0 讨论(0)
  • 2021-01-19 02:11

    I'd not bother with writing your own template tags: take things one step at a time and stick to the basics for now. There's nothing wrong with {% include 'show_pollquiz.html' %}

    0 讨论(0)
  • 2021-01-19 02:16

    Shouldn't all custom filters be inside the templatetags directory?

    templatetags/
        __init__.py
        showpollquiz.py
    

    then

    @register.inclusion_tag('show_pollquiz.html')
    

    looks in MY_TEMPLATE_DIR/show_pollquiz.html for the template

    0 讨论(0)
  • 2021-01-19 02:31

    I found the problem. The problem was that the @register.inclusion_tag('show_pollquiz.html') inclusion tag is obviously looking for the template in the default_template directory. So that is why i got the error.

    According to me this is not clear in the documentation. But I guess its how it is, being a template and all...

    Oh , well.

    Now, how would I put the @register.inclusion_tag('show_pollquiz.html') to look in the same folder as the app? under templatetags/?

    0 讨论(0)
提交回复
热议问题