Python模板和Ajax [英] Python Templating and Ajax

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

问题描述

我没能拿出这个岗位更好的标题,所以如果有人不觉得合适,请继续并对其进行编辑。

I was not able to come up with a better title for this post, so if anybody does not find it appropriate , please go ahead and edit it.

我使用的瓶作为我的Python框架,通常我渲染模板做somne​​thing像如下: -

I am using flask as my python framework, and normally I render templates doing somnething like the below:-

@app.route('/home')
def userhome():
    data=go get user details from the database
    return render_template("home.html",userdata=data)

现在我有一个模板名称home.html的中,我遍历用户数据的值,例如userdata.name,userdata.age等这些价值观把他们适当的地方在模板中。

Now I have a template name home.html in which I iterate over the values of "userdata" like userdata.name, userdata.age etc and these values take their appropriate spaces in the template.

不过我上的应用程序中,导航是通过AJAX,没有回落工作,如果JavaScript不可用(基本上是应用程序不为脂肪酶没有JavaScript的工作)。 导航菜单已经说在左边,(家庭,youroffers,yourlastreads)几个标签。右列是应该根据用户点击什么动态改变。

However I am working on an application in which navigation is via ajax and no fall back if javascript is not available(basically the app does not work for ppl without javascript). The navigation menu has say few tabs on the left ,(home,youroffers,yourlastreads). The right column is supposed to dynamically change based on what the user clicks.

我无法理解我处理模板化在这里。基于什么样的用户点击,我可以从DB通过JSON通过xhrGET或xhrPOST.Does整个模板寄给他所需要的数据都在服务器端进行处理,然后将整个模板通过Ajax调用转移。我其实不喜欢这个主意了。将是巨大的,如果有人能在正确的方向在这里点我。

I am unable to understand how I handle templating here. Based on what the user clicks I can send him the required data from the db via a json through an xhrGET or xhrPOST.Does the entire templating have to be handled at the server end and then transfer the entire template via an ajax call. I actually dont like that idea much. Would be great if someone could point me in the right direction here.

现在该是通过AJAX加载的页面,也有一些脚本,这是present。将这些脚本工作,如果通过AJAX加载。

Now in the page that is loaded via ajax , there are some scripts which are present. Will these scripts work, if loaded via ajax.

推荐答案

我会选择服务器端模板,因为除非你发现处理同一个模板语言的code是不会走干一个JS库。

I would choose server side templating, because unless you find a JS library that handles the same templating language your code isn't going go be DRY.

home.html的模板,我会做这样的事情

In the home.html template, I'd do something like

<%extends base.html%>
<%include _user_details.html%>
... <% footer and other stuff%>

和保持 _user_details.html 的实际标记。通过这种方式,为AJAX请求您刚刚呈现 _user_details.html 偏才。

And keep the actual markup in _user_details.html. This way, for an AJAX request you just render the _user_details.html partial only.

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

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