Question

I tried to use dajaxice for refreshing my table but I see an error.

Traceback:

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'

This is my view:

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)

And my js is :

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

Solution

django-dajaxice works by wrapping view functions, here index1 in your code, with special handler; and serving them through dynamically matched urls instead of /bank/index1/.
Thus you don't have to define urlpattern /bank/index1/ for the index1, just access it through Dajaxice interface, perhaps Dajaxice.bank.index1(my_callback) here.

The error was caused by the reason that a normal Django view is expected to return an HttpResponse() instance, whilst index1 returns a string here (When you call through dajaxice interface, the special handler mentioned above would put the string to an HttpResponse w/o problem)

OTHER TIPS

I modify my view and put the dajaxice function to ajax.py and this is my js for calling it:

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

I can see the script call my djaxice in console but I don't know what should i write in my_callback js function to change the table!

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

view:

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)

and my template is:

{% 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 %}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top