我正在編寫一個簡單的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文件)。我找不到任何東西來解釋這一點 - 我已經嘗試了所有我能想到的項目名稱/應用程序名稱的組合!
謝謝! :)
請幫幫忙!!「( – tominwood 2012-04-11 09:33:11