使用Djangosideb输出最近10条新闻

2024-04-26 11:48:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用django边栏(https://github.com/ekaputra07/django-sidebar)来构建我的小部件。我知道如何使用简单的变量,但是现在我不能使用对象来接收数据。 下面我提供我的代码(文件django_最新_帖子.py例如):

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')

请帮助我如何更正模板中的接收数据? 谢谢你的建议!在


Tags: djangotextfromimportdivtemplateformswidget
1条回答
网友
1楼 · 发布于 2024-04-26 11:48:06

您应该使用自定义模板标记来实现这一点。最合适的似乎是inclusion tags(但值得通读整章!)。 有一个很好的循序渐进的指导应该对你有用。在

相关问题 更多 >