视图中的Django模板标签 [英] Django Template Tags in Views

查看:81
本文介绍了视图中的Django模板标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过Ajax刷新我的自定义模板标签--right_side.py。有没有办法在视图中导入模板标签并将其作为HttpResponse返回,因为我不想放弃我的自定义模板标签(在其他页面中工作得很好),也不编写与之类似的新视图操作。



如果request.isAjax()具有使用Ajax调用的链接或将其加载到



中的视图中:



对我来说都很好

解决方案

当使用ajax刷新区域时,我觉得非常有用。所以认为分享它是很好的:



首先,您导入您在视图文件中编码的自定义模板标签。

  from your_app_name.templatetags import your_tag_name 

然后你使用它:

 返回HttpResponse(your_tag_name.your_method(context) )

这对我有用,我得到了模板标签作为服务器的响应,并刷新了div结果。


Hi I need to refresh my custom template tag --right_side.py-- via Ajax. Is there a way to import the template tag in the view and return it as HttpResponse because I don't want to give up my custom template tag (it works great in other pages) nor code a new view action which is really similar to it.

Having a link to call with Ajax or loading it in the view inside

if request.isAjax():

Are both fine for me.

解决方案

I find it really useful when refreshing an area with ajax. So thought it would be good to share it:

First you import the custom template tag you coded in your view file.

from your_app_name.templatetags import your_tag_name 

And then you use it like this:

return HttpResponse(your_tag_name.your_method(context))

That worked for me and I got the template tag as response from server and refreshed the div with that result.

这篇关于视图中的Django模板标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆