Question

I was wondering if there is a way to pass view variables directly to dajaxice?

Actually I convert the id to a json string and load them via the dajaxice function. I like to avoid passing the id this way and have to handle it as user manipulated input.

view.py

from django.shortcuts import render_to_response, redirect
from django.shortcuts import get_object_or_404
from django.template import RequestContext
from django.utils import simplejson

def site(request, slug, number):                                                                
    collection = get_object_or_404(Collection, slug=slug)
    site = get_object_or_404(Site, collection=collection, number=number)
    json_id = simplejson.dumps(site.pk)
    return render_to_response('base/site.html', 
                                                {
                                                    'site': site,
                                                    'json_id': json_id,
                                                },
                                                    context_instance=RequestContext(request))     

site.html

...
var id = {{ json_id|safe }};
var user_changes = "";
...
Dajaxice.base.submit_changes(callback, {'id': id, 'user_changes': user_changes});

ajax.py

from django.utils import simplejson
from dajaxice.decorators import dajaxice_register

@dajaxice_register
def submit_changes(request, id, user_changes):
    ...
    return simplejson.dumps({'message':'You changed:%s in Site #%s!' % (user_changes, id)})
Was it helpful?

Solution

The best way is probably to store such information in the session to prevent manipulation

view.py

...
request.session['id'] = site.pk
...

ajax.py

...
id = request.session['id']
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top