[GAE]如何在模板中设置`inline`

1 投票
1 回答
2146 浏览
提问于 2025-04-15 16:59

我之前在PHP的世界里,习惯这样创建下拉框:

<select>
<?php foreach($arrField as $idx=>$val){?>
   <option <?php echo ($fieldVal == $idx ? "selected='selected'" : ''); ?>><?php echo $val; ?></option>
<?php } ?>
</select>

但是在Python里,我没办法这样做。以下是我的代码片段:

<select name='type'>
   <option value='normal' {% if id = 'normal' %} selected="selected"{% endif %}>1-Normal</option>
   <option value='image' {% if id = 'image' %} selected="selected"{% endif %}>2-Image</option>
</select>

我遇到了这个错误:

TemplateSyntaxError: 'if' statement improperly formatted

有没有办法做到这一点呢?

1 个回答

2

如果你在使用Django框架,你需要这样做:

{% ifequal id "something"%}selected='selected'{% endifequal %}

你还需要确保“id”是一个你传递给templates.Render()的变量。

P

撰写回答