Django simple tag doesn't work in if condition

拈花ヽ惹草 提交于 2019-12-02 16:50:23

问题


I want to customize django-admin's change form for video objects by adding block with moderation tools.

When I use custom simpletags in if condition - it doesn't work.

models.py:

class Video(models.Model):

    class Meta:
        db_table = 'video'

    DRAFT = 1
    MODERATION = 2
    PUBLISHED = 3
    REJECTED = 4
    HOSTING_UPLOADING = 5
    SUSPICIOUS = 6

    PUBLICATION_STATUSES = (
        (DRAFT, 'draft'),
        (MODERATION, 'moderation'),
        (PUBLISHED, 'published'),
        (HOSTING_UPLOADING, 'hosting uploading'),
        (REJECTED, 'rejected'),
        (SUSPICIOUS, 'suspicious')
    )

    video_pk = models.AutoField(primary_key=True)
    name = models.CharField(max_length=150, blank=True)
    hosting_id = models.CharField(max_length=20, blank=True)
    publication_status = models.PositiveSmallIntegerField(choices=PUBLICATION_STATUSES, default=MODERATION)

templatetags video_publication_statuses.py:

from api.models import Video
from django import template

register = template.Library()

@register.simple_tag
def moderation(status):
    return status == Video.MODERATION


@register.simple_tag
def suspicious(status):
    return status == Video.SUSPICIOUS


@register.simple_tag
def published(status):
    return status == Video.PUBLISHED


@register.simple_tag
def hosting_uploading(status):
    return status == Video.HOSTING_UPLOADING


@register.simple_tag
def rejected(status):
    return status == Video.REJECTED

change_form.html:

{% extends "admin/change_form.html" %}
{% load video_publication_statuses %}
{% suspicious original.publication_status as suspicious_status %}
{% moderation original.publication_status as moderation_status %}
{% hosting_uploading original.publication_status as hosting_uploading_status %}
{% published original.publication_status as published_status %}
{% rejected original.publication_status as rejected_status %}

{% block after_related_objects %}
  {% if original.pk %}
    {% for fieldset in adminform %}
      {% if fieldset.name == 'Moderation' %}
        {% include "admin/includes/fieldset.html" %}
      {% endif %}
    {% endfor %}
    <div class="submit-row">
      {% if rejected_status or moderation_status or suspicious_status %}
        <input type="submit" value="Publish" name="publish" >
      {% endif %}
      {% if published_status %}
        <input type="submit" value="Reject" name="reject" >
      {% endif %}
    </div>
  {% endif %}
{% endblock %}

When I use explicit values instead of tags it works:

  {% if original.publication_status == 3 %}
    <input type="submit" value="Reject" name="reject" >
  {% endif %}

Please help me understand what is wrong with tags?


回答1:


I believe this is happening because template tags pass strings and you're checking a string against an integer e.g. return "3" == 3

Broadly speaking though, you're putting a lot of logic in a template and I typically avoid that situation. Template tags are reserved for "presentation logic" and I take that to mean "changing the way something is presented", not changing what is viewed. That logic belongs in a view or the model itself.

It should be easy enough to put this logic on your model.

class Original(...):
  def rejected(self):
    return self.status == Video.rejected      


来源:https://stackoverflow.com/questions/39556917/django-simple-tag-doesnt-work-in-if-condition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!