我尝试使用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>
答案 0 :(得分:1)
django-dajaxice
通过使用特殊处理程序将视图函数(此处为index1
)包装在代码中来工作;并通过动态匹配的网址而不是/bank/index1/
来提供服务
因此,您不必为/bank/index1/
定义urlpattern index1
,只需通过Dajaxice interface访问它,也许Dajaxice.bank.index1(my_callback)
来访问它。
错误是由于正常的Django视图预期会返回HttpResponse()
实例,而index1
在此处返回一个字符串(当您通过dajaxice接口调用时,上面提到的特殊处理程序)会把字符串放到HttpResponse
w / o问题)
答案 1 :(得分: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 })
AJAX:
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 %}