模板语法错误。无法分析余数(Django)

2024-10-03 15:30:08 发布

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

有人能给我指出正确的方向吗?下面是我一直在做的事情

    <p>{{article.snippet}}</p>
    <p>{{article.date}}</p>
    <p>{{article.retrieve_by_category("web")}}</p>

获取日期或代码片段没有问题,但在尝试运行article.retireve_by_category时,我一直收到以下错误。在

Could not parse the remainder: '("web")' from 'article.retrieve_by_category("web")'

以下是模型的代码:

^{pr2}$

Tags: 代码webdatebyparse错误articlenot
2条回答

不能使用类似于模板标记的函数

<p>{{article.snippet}}</p>
<p>{{article.date}}</p>
<p>{{article.retrieve_by_category("web")}}</p>  # this is wrong

相反,你可以让你的custom template tags。在

不能在模板内调用函数。最好在您的视图中进行处理,下面是一个简单的示例:

def your_view(request):

   article = Article.objects.filter(category="web")

   return render(request, "/toyourtemplate", {'web':article})

如果您想让类别成为动态用户输入,可以使用django表单

相关问题 更多 >