0

at views.py :

def changeCat(request, cat_key) :
    cat = Category.objects.get(pk=cat_key)
    for doc in cat.document_set.all() :
        log_changing(doc.title, 'deleted', doc.pk)
    cat.delete()
    return HttpResponseRedirect(reverse('list'))

at static js file:

$.post('/handbook/changeCat/', {"title" : $("#catTitle").val()})

I want to make $.post function accept simply what view returns, just like it acts after submitting a form, or changing URL with location.href. So I`ve tried to add .success function:

.success(function(data){
      //$('html').html(data)//worked bad for me, causing staticfiles issue
      location.reload()//it works, but without using response
  });

Why I dont like reload() method: when having a 500 error, I don`t receive that cute debug report from django, and only get this error code. Another way is to make hidden form and submit it instead of using $.post, but I hope there is more efficient and correct way.

nitroman
  • 623
  • 2
  • 8
  • 25
Robin Bobin
  • 101
  • 4
  • You can get that "cute debug report" by inspecting the response in the network tab of your browser, what it will probably tell you though is you're missing the csrf_token – Sayse May 05 '16 at 13:48
  • Im`not missing csrf token because I`m using AjaxSetup to insert it, but when inspecting 500 responce (not in the example) I see empty responce, only headers. – Robin Bobin May 05 '16 at 13:53

2 Answers2

1

You don't need an async POST here or the reload. The view is set up to respond with an HTTP 302 so it is already going to do another round trip to the server to load the redirect page.

Either use a simple form.submit() or if you want to do some processing to figure out what the POST variables and values should be then take a look at this SO question/answer.

Community
  • 1
  • 1
Jody Boucher
  • 331
  • 1
  • 7
0

In order for javascript to be able to read data sent from server in

success(function(data){

}

it must be in some form of exchangable format(xml,json). I would recommend you to use DjangoRestFramework. Check this page: http://www.django-rest-framework.org/tutorial/2-requests-and-responses/

Or you can just do something like this without need for rest framework: Django view returning json without using template.

Community
  • 1
  • 1
Milos Miletic
  • 490
  • 5
  • 18