0

I use django-sidebar(https://github.com/ekaputra07/django-sidebar) for building my widgets. I know how I can use a simple variables, however now I cannot use objects for receive data. Below I providing my code (file django_latest_posts.py as example):

from sidebar.base import SidebarWidget, sidebar_widget
from django import forms
from news.models import Post


TEMPLATE = """
<div class="last_posts">
{% for post in posts %}
   <div>{{post}}</div>
{% endfor %}
 </div>
"""

#TEMPLATE = get_template('latest_posts_widget.html')



class TextForm(forms.Form):
    text = forms.CharField(widget=forms.Textarea)
    posts = BlogPost.objects.all()[:2]  # I need to show last two posts


class LatestPosts(SidebarWidget):
    #admin_form = TextForm
    #template_text = get_template('latest_posts_widget.html')
    template_text = TEMPLATE

# register the Widget
sidebar_widget = LatestPosts('Latest Posts','Display a Latest Posts')

Please help me how I would correct receive data in TEMPLATE? Thanks for any ideas!

4

1 回答 1

0

您应该使用自定义模板标签来实现这一点。最合适的似乎是包含标签(但值得阅读整章!)。有一个很好的分步说明应该适合你。

于 2013-09-21T05:50:16.043 回答