如何调用Dajax / Dajaxice功能,从我的Django模板 [英] How do I call Dajax / Dajaxice functions from my Django template
问题描述
我写一个简单的Django应用程序,并希望增加使用Dajax / Dajaxice AJAX页面。我已经开始试图实现从Dajax网站(http://dajaxproject.com/pagination/)的简单分页的例子 - 但没有设法得到它的工作。每当我preSS下一步按钮,出现以下JS错误:
I am writing a simple Django application and wish to add ajax paging using Dajax / Dajaxice. I have started by trying to implement the simple paging example from the Dajax website (http://dajaxproject.com/pagination/) - but haven't managed to get it working. Whenever I press the "next" button I get the following js error:
Uncaught TypeError: Cannot call method 'pagination' of undefined
我的Django项目被称为DoSomething的 - 它包含一个单一的应用程序被称为核心
My Django project is called "DoSomething" - and it contains a single app called "core".
我按照所有的指示,在这里安装Dajaxice:的https:// github上。 COM / jorgebastida / Django的dajaxice /维基/安装
I have followed all of the instructions to install Dajaxice here: https://github.com/jorgebastida/django-dajaxice/wiki/installation
我在被称为ajax.py中的核心目录包含以下code的Python文件:
I have an python file in the "core" directory called "ajax.py" which contains the following code:
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文件包含下面的方法:
My views.py file contains the following method:
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
我的索引模板包含以下内容:
My index template contains the following:
<div id="pagination">
{% include "posts_paginator.html" %}
</div>
我的posts_paginator.html模板包含下面的链接,触发分页方式:
My posts_paginator.html template contains the following link, to trigger the pagination method:
{% 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文件)。我找不到任何解释这个 - !我已经试过了项目名称/应用程序名称的每个组合,我能想到的
My question is, within the onClick value, how should I be referencing the pagination method (from my ajax.py file). I can't find anything to explain this - and I've tried every combination of project name/app name that I can think of!
谢谢! :)
推荐答案
至少在我的情况下,我不得不追加的项目名称,以及该app_label。
At least in my case I have to append the Project name as well as the app_label.
Dajaxice.MyProject.core.my_dajax_method(Dajax.process, {'form' : data});
这篇关于如何调用Dajax / Dajaxice功能,从我的Django模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!