2012-04-07 42 views
1

我正在編寫一個簡單的Django應用程序,並希望使用Dajax/Dajaxice添加ajax分頁。我已經開始嘗試從Dajax網站(http://dajaxproject.com/pagination/)實現簡單的分頁示例 - 但還沒有設法使它工作。每當我按「下一步」按鈕,我得到以下JS錯誤:如何從我的Django模板中調用Dajax/Dajaxice函數

Uncaught TypeError: Cannot call method 'pagination' of undefined 

我的Django項目被稱爲「DoSomething的」 - 它包含一個名爲「核心」一個單一的應用程序。

我按照所有的指示在這裏安裝Dajaxice:https://github.com/jorgebastida/django-dajaxice/wiki/installation

我有所謂的「ajax.py」中的「核心」目錄包含以下代碼的Python文件:

from views import get_pagination_page 
from dajax.core.Dajax import Dajax 
from django.template.loader import render_to_string 
from dajaxice.decorators import dajaxice_register 
from django.utils import simplejson 

@dajaxice_register 
def pagination(request, p): 
    try: 
     page = int(p) 
    except: 
     page = 1 
    items = get_pagination_page(page) 
    render = render_to_string('posts_paginator.html', { 'items': items }) 

    dajax = Dajax() 
    dajax.assign('#pagination','innerHTML',render) 
    return dajax.json() 

我views.py文件包含以下方法:

def index(request): 
    posts = Post.objects.order_by('id').reverse() 
    items = get_pagination_page(1) 
    return render_to_response('index.html', locals(), context_instance=RequestContext(request)) 

def get_pagination_page(page=1): 
    from django.core.paginator import Paginator, InvalidPage, EmptyPage 
    from django.template.loader import render_to_string 
    items = Post.objects.order_by('id').reverse() 
    paginator = Paginator(items, 10) 
    try: 
     page = int(page) 
    except ValueError: 
     page = 1 
    try: 
     items = paginator.page(page) 
    except (EmptyPage, InvalidPage): 
     items = paginator.page(paginator.num_pages) 
    return items 

我的索引模板包含以下內容:

<div id="pagination"> 
    {% include "posts_paginator.html" %} 
</div> 

我posts_paginator.html模板中包含的鏈接,觸發分頁方法:

{% for i in items.object_list %} 
    {{ i }}<br> 
{% endfor %} 
{% if items.has_next %} 
    <a href="#" onclick="Dajaxice.core.pagination(Dajax.process,{'p':{{ items.next_page_number }}})">next</a> 
{% endif %} 

我的問題是,在價值的onClick內,我應該如何引用分頁方法(從我ajax.py文件)。我找不到任何東西來解釋這一點 - 我已經嘗試了所有我能想到的項目名稱/應用程序名稱的組合!

謝謝! :)

+0

請幫幫忙!!「( – tominwood 2012-04-11 09:33:11

回答

2

至少在我的情況下,我必須追加項目名稱以及app_label。

Dajaxice.MyProject.core.my_dajax_method(Dajax.process, {'form' : data}); 
+0

我想試試,看看它的工作原理感謝 – tominwood 2012-04-11 10:08:59

+0

耶!!!(這工作):) – tominwood 2012-04-11 22:07:45

+0

注意:http://stackoverflow.com/questions/26219391/dajax - 不工作/ 29230482#29230482 – Trix 2015-05-11 13:29:00