Django和Ajax - 我该怎么办? [英] Django and Ajax - What can I do?

查看:141
本文介绍了Django和Ajax - 我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎完成了我的网站,除了最后一部分,我需要使画廊页面支持ajax使用Ajax更改页码。

I'm almost done with my site except for the last part where I need to make the gallery page support ajax to change the page number using Ajax.

画廊页面视图:

def gallerypages(request, page):
    items = Example.objects.all().order_by('-pk')
    categories = Categorie.objects.all()

    paginator = Paginator(items, 12)

    try:
       itemsList = paginator.page(page)
    except PageNotAnInteger:
       itemsList = paginator.page(1)
    except EmptyPage:
       itemsList = paginator.page(paginator.num_pages)

    if items.count()>1:
       return render_to_response('gallery.html', {'items': itemsList,'categories': categories,}, context_instance = RequestContext(request))

Dajax / Dajaxice没有很好的记录...我只需要显示一些图像。 p>

Dajax/Dajaxice are not very well documented... I only need to show some images.

推荐答案

这里是ho要用Dajax / Dajaxice来做,这意味着在Django中使AJAX变得简单:

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应用程序,并将必要的Javascript库加载到 gallery.html 中。 (请注意,您需要安装jQuery或类似的JS框架才能使Dajax工作。)

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 中的部分具有特定ID的空白< div> ,例如

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>

您正在做的是创建#gallery-content 作为通过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 代码 - 这被称为每当用户点击按钮元素 - 做两件事情:(1)获取#page-number 输入元素的值,(2)将其发送到通过 Dajaxice.myapp.gallerypages_content Javascript调用,Django gallerypages_content 异步查看,即无需正常的Web浏览器页面加载。请注意, 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 视图 - 这是您修改为使用Dajaxice / Dajax的现有 gallerypages 视图的变体。 Dajaxice是硬编码,以便在 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()

这是上面的代码确实:(1)转换页面参数,现在是一个字符串(即将原始字符串值#page-number input元素)转换为Python整数; (2)执行与之前相同的计算,以获得 itemsList 类别; (3)使用 render_to_string gallery_content.html 呈现给HTML字符串而不是正常的Django HTTP响应; (4)使用Dajax API创建一个将HTML注入到#gallery-content div中的指令; (5),并且作为视图的响应,以JSON格式返回这些指令。在 onclick 处理程序中的Dajaxice调用将实际上将收到这些指令并对它们采取行动(严格来说,它是 Dajax.process 回调,这样做),导致HTML出现。请注意,您需要使用 @dajaxice_register 来装饰 gallerypages_content - 这有助于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!

这篇关于Django和Ajax - 我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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