1

我尝试使用 dajaxice 来刷新我的表格,但我看到了一个错误。

追溯:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/bank/index1/

Django Version: 1.5.1
Python Version: 2.7.3
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'meli',
 'django_tables2',
 'dajaxice',
 'django_jalali')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/usr/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  187.                 response = middleware_method(request, response)
File "/usr/lib/python2.7/site-packages/django/contrib/sessions/middleware.py" in process_response
  26.                 patch_vary_headers(response, ('Cookie',))
File "/usr/lib/python2.7/site-packages/django/utils/cache.py" in patch_vary_headers
  142.     if response.has_header('Vary'):

Exception Type: AttributeError at /bank/index1/
Exception Value: 'str' object has no attribute 'has_header'

这是我的看法:

def index1(request):

    data = MelliTable(ModelMelli.objects.filter(check=False))
    RequestConfig(request, paginate={'per_page': 20}).configure(table)
    table = render_to_string('meli/index.html', {'data': data })
    return simplejson.dumps({'table':table})
dajaxice_functions.register(index1)

我的js是:

<script>
function my_callback(data){
    if(data!=Dajaxice.EXCEPTION){
        document.getElementById('test').innerHTML = data.table;
    }
    else{
        alert('Error');
    }
}
</script>
4

2 回答 2

1

django-dajaxice通过index1在代码中使用特殊处理程序包装视图函数来工作;并通过动态匹配的 url 而不是/bank/index1/.
因此,您不必为 定义 urlpattern /bank/index1/index1只需通过Dajaxice 接口访问它,也许Dajaxice.bank.index1(my_callback)在这里。

错误是由于一个普通的Django视图应该返回一个HttpResponse()实例,而index1这里返回一个字符串(当你通过dajaxice接口调用时,上面提到的特殊处理程序会将字符串置于HttpResponsew/o问题)

于 2013-06-25T13:55:12.510 回答
0

我修改了我的视图并将 dajaxice 函数放到 ajax.py 中,这是我调用它的 js:

<script>
function setupRefresh() {
    setTimeout("Dajaxice.meli.refresh(my_callback)", 5000); // milliseconds
}
</script>

我可以在控制台中看到脚本调用我的 djaxice,但我不知道我应该在 my_callback js 函数中写什么来更改表!

5/Jun/2013 23:25:43] "POST /dajaxice/meli.refresh/ HTTP/1.1" 200 2580
[25/Jun/2013 23:25:48] "POST /dajaxice/meli.refresh/ HTTP/1.1" 200 2580
[25/Jun/2013 23:25:53] "POST /dajaxice/meli.refresh/ HTTP/1.1" 200 2580
[25/Jun/2013 23:25:58] "POST /dajaxice/meli.refresh/ HTTP/1.1" 200 2580
[25/Jun/2013 23:26:03] "POST /dajaxice/meli.refresh/ HTTP/1.1" 200 2580

看法:

def index2(request):

    table = MelliTable(ModelMelli.objects.filter(check=True))

    #table.paginate(page=request.GET.get('page', 1), per_page=4)
    RequestConfig(request, paginate={'per_page': 20}).configure(table)
    return render(request, 'meli/index1.html', {'table': table })

阿贾克斯:

def refresh(request):
    message = ""
    table = MelliTable(ModelMelli.objects.filter(check=True))
    RequestConfig(request, paginate={'per_page': 20}).configure(table)
    table1 = render_to_string('meli/index.html', {'table': table, 'message': message })
    return simplejson.dumps({'table1':table1})

dajaxice_functions.register(refresh)

我的模板是:

{% extends "meli/base.html" %}
{% load render_table from django_tables2 %}
{% block content %}
<script>
function setupRefresh() {
    setTimeout("Dajaxice.meli.refresh(my_callback)", 5000); // milliseconds
}
</script>

            {% render_table table %}            
{% endblock %}
于 2013-06-26T04:27:44.517 回答