How do I call Dajax / Dajaxice functions from my Django template

微笑、不失礼 提交于 2019-12-08 04:26:14

问题


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

My Django project is called "DoSomething" - and it contains a single app called "core".

I have followed all of the instructions to install Dajaxice here: https://github.com/jorgebastida/django-dajaxice/wiki/installation

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()

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>

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 %}

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!

THANKS! :)


回答1:


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});


来源:https://stackoverflow.com/questions/10053660/how-do-i-call-dajax-dajaxice-functions-from-my-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!