2012-07-29 89 views
1

我几乎完成了我的网站,除了最后一部分,我需要使画廊页面支持Ajax使用Ajax更改页码。Django和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没有很好的记录。我只需要显示一些图像。

回答

10

以下是如何与Dajax/Dajaxice,这是为了使AJAX容易在Django中做到这一点:

  1. 安装DajaxiceDajax每个文档。该文档似乎没有提到它,但你也可以使用pip,即

    pip install django-dajaxice 
    pip install django-dajax 
    

    到拿到库。无论如何,请确保按照文档说明安装Django应用程序并获取加载到gallery.html的必要Javascript库。 (请注意,您需要有jQuery的或安装Dajax工作类似的JS框架。)

  2. gallery.html,隔离在itemscategories被渲染成HTML的部分。将此部分复制到名为gallery_content.html的独立Django模板中,然后用中的部分替换为空白<div>,其中使用特定的id,例如。

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

    你正在做什么是创造#gallery-content为,将通过Dajaxice呼叫后为每个页面生成HTML的占位符。

  3. 现在,在gallery.html的其他地方,为用户创建一种方式告诉您要去哪个页面,例如,

    <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)将其发送到Django gallerypages_content查看异步,即没有正常的网页浏览器页面加载,通过调用Dajaxice.myapp.gallerypages_content。请注意,myapp应该替换为您的Django应用程序的名称。

  4. 最后,您需要创建gallerypages_content视图 - 这是您现有的gallerypages视图的一个变体,已修改为与Dajaxice/Dajax一起使用。 Dajaxice是硬编码为寻找这样的观点在ajax.py,所以创建ajax.pymyapp文件夹,如下所示:

    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参数,它现在是一个转换字符串(即#page-number输入元素的原始字符串值)转换为Python整数; (2)按照与前面相同的计算得到itemsListcategories; (3)使用render_to_stringgallery_content.html呈现为HTML字符串,而不是正常的Django HTTP响应; (4)使用Dajax API创建一条将HTML注入#gallery-content div的指令; (5),并且作为视图的响应,以JSON格式返回这些指令。onclick处理程序中的Dajaxice调用实际上会接收这些指令并对其执行操作(严格来说,这是执行此操作的Dajax.process回调),导致HTML显示出来。请注意,您需要用@dajaxice_register装饰gallerypages_content - 这有助于Dajaxice将所有内容都集中在一起。

我没有测试过任何这明确,但它是基于我如何得到Dajaxice/Dajax为我工作,我希望它为你工作 - 或者至少让你开始!

+0

非常感谢,我现在好了我该做的明白。我仍然有1个问题,我不断收到一个js警报框,提示“出现问题”。我为Dajaxice设置了处理程序和记录器,出现此错误: Traceback(最近调用最后一次): 文件“/Library/Python/2.7/site-packages/dajaxice/core/DajaxiceRequest.py”,第181行在过程 响应= '%s' 的%thefunction(self.request获取,**的argv) 类型错误:gallerypages()究竟需要两个参数(1给出) 响应:DAJAXICE_EXCEPTION 的什么是错的任何想法?这些函数接受一个请求和一个页面参数。 – moenad 2012-07-30 10:07:28

+0

糟糕,我错误地给Dajaxice写了'onclick'调用。我已经(希望)修复它;再试一次。 – Ghopper21 2012-07-30 12:36:56

+0

嗯,你的意思是我上面的代码中的方法已被弃用?从我可以看到的[这里](https://github.com/jorgebastida/django-dajax/wiki),它看起来像标准的路要走。也许我在看错版本? – Ghopper21 2012-07-30 14:00:52

1

Should I use django-dajax or django-dajaxice?

In a word, No. I created these projects 4 years ago as a cool tool in order to solve one specific problems I had at that time.

These days using these projects is a bad idea.

https://github.com/jorgebastida/django-dajax/