Search code examples
pythondjangodajaxice

dajaxice error 'str' object has no attribute 'has_header'


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>

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)