从模板调用模型上的方法

iJK*_*iJK 8 django django-templates

我试图从模板中调用模型中的方法,我得出结论,这是不可能做到的.

这是我的代码

{% if request.user.is_authenticated %}
  {% if a_story.is_story_liked(request.user.id) %}
    <a class="story_like" data-id="{{ a_story.id }}" href="#">Like</a>
  {% endif %}
  {% else %}
    <a class="story_like_login" data-id="{{ a_story.id }}" href="#">Like</a>
{% endif %}
Run Code Online (Sandbox Code Playgroud)

错误发生在第二行."is_story_liked"检查用户是否"喜欢"了一个故事.如果没有,那么我会写相同的锚标签但是使用不同的类.

我有点难过这个.我正在尝试输出不同的类名:如果用户已登录,如果用户未登录,并且用户已"喜欢"或未"喜欢"文章/故事.

Ber*_*ant 21

django模板中的方法调用只有在没有参数时才有效(例如{% if request.user.is_authenticated %}).您需要将该功能放在呈现此模板的视图中,或将此功能放在自定义模板标记中.