Skip to main content
 首页 » 编程设计

python之Django之模板或 View 中的标记解析器

2024年09月03日39duanxz

我正在构建一个网站,我的页面是用 MediaWiki 标记编写的,为此我在 Python 中有一个可用的解析器函数。

我究竟在哪里解析我的标记:在 View 的代码中,还是在模板中?我的第一个猜测是这样的:

return render_to_response( 'blog/post.html', {'post': post, 
                           'content': parseMyMarkup(post.content) }) 

这是通常的惯例,还是我应该做些不同的事情?

请您参考如下方法:

决定是否将代码放在 View 或模板中的一般规则是:

If your code is going to modify the data, put it into the view. If your code will only effect the display of the data, put it into the template.

我不是很熟悉标记格式,但如果您要执行替换(例如:**word** becomes <b>word</b>),那么我会把它放入 View 中,因为它会修改您的数据。

希望对您有所帮助!