一个更好的方式做阿贾克斯在Django [英] a better way to do ajax in django

查看:122
本文介绍了一个更好的方式做阿贾克斯在Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一天,我写了一些AJAX的Django的应用程序,我一直在努力。

The other day I wrote some AJAX for a Django app that i have been working on.

我来自Ruby on Rails的,所以我没有生JS的方式做了很多。

I come from Ruby on Rails, so I haven't done much in the way of raw JS.

因此​​,基于Rails的谐音,我的东西类似于一种伪code以下,不出汗的细节:

So based on Rails' partials, I something similar to the following in a sort of pseudocode, don't sweat the details:

1),使用JS函数原型的Ajax.Updater('tablediv'是我想更新Ajaxily表的ID和URL指向正确的Django的视图)

1) JS function using prototype's Ajax.Updater ('tablediv' being the id of the table i wanted to update Ajaxily, and url pointing to the proper django view)

 function updateTable(){
       new Ajax.Updater('tablediv',url {params: params....etc

2),进行了新的数据来填充表Django的观点:

2) django view that got new data to populate the table with:

 def ajaxTable
     objects = Objects.object.all...
     return render_to_response('ajaxtable.html',objects)

3)ajaxtable.html只是一种滑轨的部分所以基本表的w / o <表> < /表> ...

   <th>{{object.data}}</th>
   <td>{{object.moredata}}</td>

所以我实际的问题:

so to my actual question:

这似乎哈克给我,我厌倦了什么我想在网上搜索后的排序一起扔了。

This seemed hacky to me, I sort of threw it together after getting tired of searching online for what i wanted.

这是它的工作方式?它工作正常,我只是不知道不够了解,你知道吗?

Is this the way it's done? It works fine, I just don't know enough to know, you know?

推荐答案

这有点取决于你想要做的我觉得。阿贾克斯是相当广泛的,从谷歌地图场景,一个简单的自动完成瓦里斯极大的复杂性和最好的办法。

It kinda depends what you want to do I think. Ajax being quite a wide range of scenarios from Google Maps to a simple auto-complete varys greatly in complexity and the best approach.

然而,也有一些有用的东西,你可以做到这一点的帮助。

However, there are some useful things you can do that help.

1)模板级别

请确保你在你的TEMPLATE_CONTEXT_PROCESSORS设置django.core.context_processors.request。然后,你可以做到这一点;

Make sure you have "django.core.context_processors.request" in your TEMPLATE_CONTEXT_PROCESSORS setting. Then you can do this;

{% if not request.is_ajax %}
<html>
  <head>
  ...
  </head>
  <body>
  ...
{% endif %}
actual content
{% if not request.is_ajax %}
</body>
</html>
{% endif %}

基本上,然后说这页/测试/你可以做一个浏览器请求并获得通过JavaScript的全部内容或请求和刚刚获得的内容。有一个博文的地方,解释得更详细些,但我无法找到它的时刻。

Basically then say this page is /test/ you can do a browser request and get the full content or a request via JavaScript and just get the content. There is a blogpost somewhere that explains this in more detail but I can't find it at the moment.

2)在视图

在模板中,我们只是访问模板中的请求对象。在视图中,可以做的非常类似的事情。

In the template we are just accessing the request object in the template. In the view you can do very similar things.

def my_view(request):
    if requst.is_ajax():
        # handle for Ajax requests

    # otherwise handle 'normal' requests
    return HttpResponse('Hello world')

以上方法没有真正做到这一点比你做的不同,但允许你重新使用的观点,并写一点更简洁。我真的不能说你在做什么是错的或者哈克,但你可以写它,使之更简洁,再利用模板和意见。

The above methods don't really do it differently than you do but allow you to re-use views and write it bit more concisely. I wouldn't really say what you are doing is wrong or hacky but you could write it to make it more concise and re-use the templates and views.

说,例如,你可能只是一个模板,如果它是一个Ajax请求有它只有返回,将需要更新的部分。你的情况这将是表意见。

say for example you could have just one template and if its a Ajax request have it only return the section that will need to be updated. In your case it would be the tables views.

这篇关于一个更好的方式做阿贾克斯在Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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