简体   繁体   English

在Django中使用Python格式化字符串

[英]Formatting strings with python in django

I have a question list for a questionnaire which are stored in database but some questions has to be modified by certain parameters. 我有一个问卷调查表的问题清单,该清单存储在数据库中,但是某些问题必须通过某些参数进行修改。

For example, if someone selects an employer name from previous page, some questions should have employer's name "Do you like to work for ........ company ?". 例如,如果某人从上一页中选择了雇主名称,则某些问题应具有雇主名称“您是否想为........公司工作?”。

One solution might be savinf question like "Do you like to work for {0}" and formatting it but I am not sure how I can implement it with python. 一种解决方案可能是诸如“您愿意为{0}工作吗”之类的savinf问题并对其进行格式化,但是我不确定如何用python实现它。

But how can I detect which questions need to be modified ? 但是,如何检测需要修改的问题呢?

Is there any easy way to do it in django ? 在django中有什么简单的方法吗?

Thanks 谢谢

The most flexible way - is to create custom template filter. 最灵活的方法-是创建自定义模板过滤器。 If string needs formatting, it will do that, if it doesn't - just output it. 如果字符串需要格式化,那么它将执行该操作,如果不需要,则将其输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM