datatables - csrf token missing in django ajax post request working using get request -


i working metronic datatables in have file ajax function works. problem when use type in ajax function "get" works in post not work , gives csrf token missing error in console, in case of not give error, using django framework site , ajax function :-

"ajax": { // define ajax settings     "url": document.url, // ajax url     "type": "post", // request type     "timeout": 20000,     "data": function(data) { // add request parameters before submit         $.each(ajaxparams, function(key, value) {             data[key] = value;         });         metronic.blockui({             message: tableoptions.loadingmessage,             target: tablecontainer,             overlaycolor: 'none',             cenrery: true,             boxed: true         });     }, } 

urls.py file :

from django.conf.urls import url . import views  urlpatterns = [     url(r'^$', views.index, name='index'),     url(r'^logout$', views.logout, name='logout'),     url(r'^dashboard$', views.dashboard, name='dashboard'),     url(r'^profile$', views.profile, name='profile'),     url(r'^edit-profile$', views.edit_profile, name='edit-profile'),     url(r'^check-password$', views.check_password, name='check-password'),     url(r'^help$', views.faq_management, name='help'),     url(r'^testing$', views.testing_database, name='testing'),     url(r'^add-faq$', views.add_faq, name='add-faq') ] 

view related function :

from django.http import httpresponse django.shortcuts import render, redirect django.core.exceptions import objectdoesnotexist models import admin, django.contrib import messages django.utils.html import escape .forms import imageuploadform import json datetime import datetime  def faq_management(request): if 'admin_id' in request.session:     if request.method == 'get':         if request.is_ajax():             ajax_data = request.get             if ajax_data['length'] !=-1 :                 limit = ajax_data['length']             else :                 limit="all"             questions = help.objects.all().filter().values('id','question','description','status','created','modified').order_by('-id')             datalist = []             i=1;             que in questions:                 if(que['status']=='1'):                     checked='on'                 else:                     checked='off'                 actionvalues='<a title="edit" class="btn btn-sm green margin-top-10" href=""> <i class="fa fa-edit"></i></a>';                 inner_data_list = [                     i,                     que['question'],                      (que['description'][:150] + '..') if len(que['description']) > 150 else que['description'],                     '<div id=%s class="bootstrap-switch  bootstrap-switch-%s  bootstrap-switch-wrapper bootstrap-switch-animate toogle_switch"><div class="bootstrap-switch-container" ><span class="bootstrap-switch-handle-on bootstrap-switch-primary">&nbsp;active&nbsp;&nbsp;</span><label class="bootstrap-switch-label">&nbsp;</label><span class="bootstrap-switch-handle-off bootstrap-switch-default">&nbsp;inactive&nbsp;</span></div></div>'%(que['id'],checked),                     que['created'],                     que['modified'],                     actionvalues                 ]                 datalist.append(inner_data_list)                 += 1               itotalrecords=questions.count()             idisplaylength = int(ajax_data['length']);             idisplaystart = int(ajax_data['start']);             if idisplaylength < 0 :                 idisplaylength = itotalrecords             secho = int(ajax_data['draw'])             records = {}             records['data'] = {}             records['data'] = {}             records['data'] = datalist             records['customactionstatus'] = {}             records['customactionmessage'] = {}             records['draw'] = {}             records['recordstotal'] = {}             records['recordsfiltered'] = {}             if request.get.get('customactiontype', '') == 'group_action':                 records['customactionstatus'] = 'ok'                 records['customactionmessage'] = 'group action has been completed. done!'             records["draw"] = secho             records["recordstotal"] = itotalrecords             records["recordsfiltered"] = itotalrecords             return httpresponse(json.dumps(records, default=json_serial))          admin = admin.objects.get(pk = request.session["admin_id"])         return render(request, 'admin/faq-manage.py', {             'admininfo': admin,         }) else:     messages.add_message(request, messages.error, 'error! kindly login first.')     return redirect(index)   

you don't error get because csrf tokens required post requests.

check out topic in docs - https://docs.djangoproject.com/en/dev/ref/csrf/


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -