Python模板和Ajax


                    

我无法为此帖子找到更好的标题,因此,如果有人认为不合适,请继续进行编辑。</ p>

我将flask作为我的python框架,通常我渲染模板,如下所示:-</ p>

  @ app.route('/ home')
def userhome():
     data = go从数据库获取用户详细信息
     返回render_template(“ home.html”,userdata = data)
</ code> </ pre>

现在,我有一个模板名称home.html,在其中,我可以对“ userdata”的值(如userdata.name,userdata.age等)进行迭代,并且这些值在模板中占据其适当的空格。</ p>

但是,我正在开发一个通过ajax进行导航的应用程序,并且在没有javascript的情况下没有回退(基本上,该应用程序不适用于没有javascript的ppl)。
导航菜单左侧有几个选项卡,(首页,您的要约,您的最后阅读)。 右边的栏应该根据用户的点击动态地改变。</ p>

我无法理解如何在此处处理模板。 根据用户单击的内容,我可以通过xhrGET或xhrPOST通过json从db向数据库发送所需的数据,是否必须在服务器端处理整个模板,然后通过ajax调用传输整个模板。 我实际上不太喜欢这个主意。 如果有人可以在这里向我指出正确的方向,那就太好了。 </ p>

现在通过ajax加载的页面中,存在一些脚本。 如果通过ajax加载,这些脚本是否可以工作。</ p>
     </ div>

展开原文

原文

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.

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)

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.

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.

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.

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

2个回答


您有两个选择:服务器上的模板或浏览器中的模板。</ p>

要在服务器中创建模板,请像创建模板一样创建一个终结点,只是模板仅创建页面的一部分。 然后,您通过Ajax调用访问URL,并将返回的HTML插入页面中的某个位置。</ p>

要在浏览器中创建模板,您的端点将创建JSON响应。 然后,Javascript模板库可以使用该JSON,从中创建HTML,然后将其插入页面。 例如,有很多jQuery模板解决方案。</ p>
     </ div>

展开原文

原文

You have two options: template on the server, or template in the browser.

To template in the server, you create an endpoint much like you already have, except the template only creates a portion of the page. Then you hit the URL with an Ajax call, and insert the returned HTML somewhere into your page.

To template in the browser, your endpoint creates a JSON response. Then a Javascript templating library can take that JSON, create HTML from it, and insert it into the page. There are lots of jQuery templating solutions, for example.


我会选择服务器端模板,因为除非您找到处理相同模板语言的JS库,否则您的代码将无法使用。</ p>

home.html </ code>模板中,我会做类似的事情</ p>

  <%扩展base.html%>
<%include _user_details.html%>
... <页脚和其他填充物%>
</ code> </ pre>

并将实际标记保留在 _user_details.html </ code>中。 这样,对于AJAX请求,您只需呈现 _user_details.html </ code>部分。</ p>
     </ div>

展开原文

原文

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.

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

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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐