且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Django和阿贾克斯 - 我能做些什么?

更新时间:2023-11-05 16:42:22

下面是如何使用Dajax / Dajaxice,这是为了让AJAX深入浅出Django中做到这一点:

Here's how to do it with Dajax/Dajaxice, which are meant to make AJAX easy in Django:

  1. 安装 Dajaxice 和的每页文档Dajax 。该文档似乎并没有提到它,但你也可以使用 PIP ,即

  1. Install Dajaxice and Dajax per the documentation. The docs don't seem to mention it, but you can also use pip, i.e.

pip install django-dajaxice
pip install django-dajax

到获得库。在任何情况下,一定要按照说明安装Django的应用程序,并得到加载到 gallery.html 必要的JavaScript库的文档说明。 (请注意你需要有jQuery的或安装Dajax类似的JS框架的工作。)

to the get the libraries. In any case, make sure to follow the doc instructions to install the Django apps and get the necessary Javascript libraries loaded into gallery.html. (Note you need to have jQuery or a similar JS framework installed for Dajax to work.)

gallery.html ,隔离部分,其中项目类别被渲染到HTML中。本节复制到一个单独的Django模板调用,比如, gallery_content.html ,然后替换 gallery.html 部分一个空白的< D​​IV> 与特定的ID,如:

In gallery.html, isolate the section where the items and categories are rendered into HTML. Copy this section into a separate Django template called, say, gallery_content.html and then replace the section in gallery.html with a blank <div> with a specific id, e.g.

<div id="gallery-content"></div>

你在做什么是创造#画廊含量作为一个占位符,稍后会为每个页面通过Dajaxice调用来生成的HTML。

What you are doing is creating #gallery-content as a placeholder for the HTML that will be generated later for each page via a Dajaxice call.

现在,在 gallery.html 其他地方,为用户创造一个方式来告诉你去哪个页面,例如:

Now, elsewhere in gallery.html, create a way for the user to tell you what page to go to, e.g.

<input id="page-number">
<button onclick="Dajaxice.myapp.gallerypages_content(Dajax.process, {'page': document.getElementById('page-number').value})">Go to page</button>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

中的JavaScript 的onclick code - 这就是所谓每当用户点击该按钮元素 - 做了两件事:(1)抓住的价值#页号 input元素,以及(2)将其发送到Django的 gallerypages_content 视图异步,即没有一个正常的Web浏览器的页面加载,通过 Dajaxice.myapp.gallerypages_content JavaScript调用。需要注意的是的myapp 应替换为您的Django应用程序的名称。

The Javascript onclick code -- which is called whenever the user clicks on the button element -- does two things: (1) grabs the value of the #page-number input element, and (2) sends it to the Django gallerypages_content view asynchronously, i.e. without a normal web browser page load, via the Dajaxice.myapp.gallerypages_content Javascript call. Note that myapp should be replaced with the name of your Django app.

最后,你需要创建 gallerypages_content 查看 - 这是现有的 gallerypages 的变体>视图的修改与Dajaxice / Dajax工作。 Dajaxice是硬codeD来寻找这样的观点在 ajax.py ,所以创建 ajax.py 的你的的myapp 文件夹如下:

Finally, you need to create the gallerypages_content view -- which is a variant of your existing gallerypages view modified to work with Dajaxice/Dajax. Dajaxice is hard-coded to look for such views in ajax.py, so create ajax.py in your myapp folder as follows:

from django.template.loader import render_to_string
from dajax.core import Dajax
from dajaxice.decorators import dajaxice_register

@dajaxice_register
def gallerypages_content(request, page):

    page = int(page)

    # ... code to calculate itemsList and categories as before ...

    html = render_to_string('gallery_content.html',
                            {'items': itemsList,'categories': categories,}, 
                            context_instance = RequestContext(request))
    dajax = Dajax()
    dajax.assign('#gallery-content', 'innerHTML', html)
    return dajax.json()

这是什么code以上的功能:(1)参数,它现在是一个字符串(的即原始字符串值#页号 input元素),到一个Python整数; (2)做相同的计算之前,要获得 itemsList 类别; (3)使用 render_to_string 渲染 gallery_content.html 来的HTML字符串,而不是正常的Django的HTTP响应; (4)使用Dajax API来创建一个指令到HTML注入#画廊含量 DIV; (5),作为视图的反应,以JSON格式返回这些指令。在的onclick 处理的Dajaxice通话将有效得到他们(严格来说,这是 Dajax.process $ C $这些指令和行动C>的回调,这是否),使HTML展现出来。请注意,您需要装饰 gallerypages_content @dajaxice_register - 帮助Dajaxice勾得不得了

This is what the code above does: (1) converts the page parameter, which is now a string (i.e the raw string value of the #page-number input element), into a Python integer; (2) does the same calculation as before to get itemsList and categories; (3) uses render_to_string to render gallery_content.html to an HTML string instead of to the normal Django HTTP response; (4) uses the Dajax API to create an instruction to inject the HTML into the #gallery-content div; (5) and, as the view's response, returns these instructions in JSON format. The Dajaxice call in the onclick handler will in effect receive these instructions and act on them (strictly speaking, it's the Dajax.process callback that does this), causing the HTML to show up. Note that you need to decorate gallerypages_content with @dajaxice_register -- that helps Dajaxice hook everything together.

我没有测试过任何这特别,但它的基础上如何我已经得到了Dajaxice / Dajax为我工作,我希望它为你 - 或者至少让你开始!

I haven't tested any of this specifically, but it's based on how I've gotten Dajaxice/Dajax to work for me and I hope it works for you -- or at least gets you started!