- {% if user.id = user_id|add:0 %}Hello, User ID: {{ user_id }}{% endif %}
Sometimes you need to compare the ID/PK of an object with some value that comes from db, form field, etc., but they aren’t both integers and comparison fails.
A good way to solve this problem is adding zero to the string element using the filteradd
, then Django will convert to integer first.
原文地址:django模版中比较字符串和整型, 感谢原作者分享。 世界会向那些有目标和远见的人让路(冯两努——香港着名推销商