Skip to main content
 首页 » 编程设计

python之如何格式化这样的字符串

2025年05月04日26webabcd

我有一个要呈现的模板,但由于它也有一些标签,因此很难访问特定的字符串。代码如下:

   template=""" 
   <select> 
   <option {% if record.views = '%s' %} selected {% endif %}>'%s' 
   </select> 
   """%(pop, pop) 

这里我想要 pop 的值,但它给出了一个错误:

    Caught TypeError while rendering: not enough arguments for format string 

任何解决方案如何访问这些字符串格式。 谢谢

请您参考如下方法:

说真的,不要尝试预处理模板语言。这是一种模板语言!它处理这类事情!

将 selected_type 发送到模板上下文中,然后执行:

<option {% if record.views = selected_type %} selected {% endif %}>'{{ selected_type }}'