jquery - django request ajax post not working -
i trying delete object database using ajax. happening in view, request.is_ajax() returning false always. don't know why happening because used same jquery code other project working.
here code:
jquery:
$('.delete').on('click', function(e){ e.preventdefault(); console.log("clicked me"); var clicked = $(this); parent = clicked.parents('.attribute'); var type = parent.find('input[type=hidden]').val(); console.log("type", type); $.ajax({ type: 'post', url: location.href, data: {'delete': true, 'product_attr_type': type}, beforesend: function(){ console.log('posting'); } }).success(function(response, textstatus, jqxhr){ parent.hide('slow'); }).error(function(response, textstatus, jqxhr){ console.log(response); }); });
html:
<form class="smart-form" method="post" action=""> {% csrf_token %} <div class="row"> <section class="col col-2"> <label class="label"> product attributes </label> {% if product_attributes %} {% attributes in product_attributes %} <div class="col attribute"> <input type="hidden" value="{{ attributes.attribute_type }}"> attribute type<input type="text" value="{{ attributes.attribute_type }}" name="type[]"> attribute value<input type="text" value="{{ attributes.value }}" name="value[]"> <div class="delete" id="delete_{{ image.pk }}" style="float: right"><i><a>x</a></i></div> </div> {% endfor %} {% endif %} </section> </div> </form>
view:
def edit_product(request, *args, **kwargs): template = 'catalog/edit_product.html' print "request", request.is_ajax() try: product_pk = kwargs.get('product_pk') product = product.objects.get(pk=product_pk) except productdoesnotexist: raise productdoesnotexist('matching product not exist.') if request.method == 'get': product_form = editproductform(instance=product) attributes = product.product_attributes homecoming render(request, template, {'form': product_form, 'product_attributes': attributes}) elif request.method == 'post': print "going check ajax req" if request.is_ajax(): print "ajax req" if request.post.get('delete'): product_attr_type = request.post.get('product_attr_type') print "product_attr_type", product_attr_type #product.deactivate_product_attribute(product_attr_type) product.is_active = false product.save() homecoming get_json_response({'msg': 'product attribute deleted successfully'}) else: attr_type_list = request.post.getlist('type[]') attr_value_list = request.post.getlist('value[]') attribute_types = request.post.getlist('attribute_type[]') attribute_values = request.post.getlist('attribute_value[]') product_form = editproductform(request.post, instance=product) if product_form.is_valid(): form = product_form.save(commit=false) type, value in itertools.izip_longest(attr_type_list, attr_value_list): form.update_or_create_product_attribute(type, value) form.save() homecoming render(request, template, {'form_saved': 'product updated successfully!!'}) else: homecoming render(request, template, {'form': product_form})
as can notice, have csrf_token in html form. view working normal post ajax post not working.. have been trying find problem since morning not working.
please help.
request :
request false <wsgirequest path:/catalog/products/5459c34218a3b30fa82ce983/edit/, get:<querydict: {}>, post:<querydict: {}>, cookies:{'csrftoken': 'xgmtkyu29bkba2gkvo5zjblntrzbz3uz'}, meta:{'celery_loader': 'djcelery.loaders.djangoloader', 'classpath': '/home/ankita/tar_files/pycharm-3.4.1/bin/../lib/bootstrap.jar:/home/ankita/tar_files/pycharm-3.4.1/bin/../lib/extensions.jar:/home/ankita/tar_files/pycharm-3.4.1/bin/../lib/util.jar:/home/ankita/tar_files/pycharm-3.4.1/bin/../lib/jdom.jar:/home/ankita/tar_files/pycharm-3.4.1/bin/../lib/log4j.jar:/home/ankita/tar_files/pycharm-3.4.1/bin/../lib/trove4j.jar:/home/ankita/tar_files/pycharm-3.4.1/bin/../lib/jna.jar', 'compiz_config_profile': 'ubuntu', 'content_length': '', 'content_type': 'text/plain', u'csrf_cookie': u'xgmtkyu29bkba2gkvo5zjblntrzbz3uz', 'dbus_session_bus_address': 'unix:abstract=/tmp/dbus-bghj7ypaa4,guid=edd262eeb415f7bed2434b1100000065', 'defaults_path': '/usr/share/gconf/ubuntu.default.path', 'desktop_session': 'ubuntu', 'display': ':0', 'django_settings_module': 'catalog_service.settings', 'gateway_interface': 'cgi/1.1', 'gdmsession': 'ubuntu', 'gio_launched_desktop_file': '/home/ankita/.local/share/applications/jetbrains-pycharm.desktop', 'gio_launched_desktop_file_pid': '2385', 'gnome_desktop_session_id': 'this-is-deprecated', 'gnome_keyring_control': '/tmp/keyring-tx9ufr', 'gnome_keyring_pid': '1930', 'gpg_agent_info': '/tmp/keyring-tx9ufr/gpg:0:1', 'home': '/home/ankita', 'http_accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'http_accept_encoding': 'gzip,deflate,sdch', 'http_accept_language': 'en-us,en;q=0.8', 'http_cache_control': 'max-age=0', 'http_connection': 'keep-alive', 'http_cookie': 'csrftoken=xgmtkyu29bkba2gkvo5zjblntrzbz3uz', 'http_host': 'localhost:8000', 'http_referer': 'http://localhost:8000/catalog/products/', 'http_user_agent': 'mozilla/5.0 (x11; linux x86_64) applewebkit/537.36 (khtml, gecko) chrome/38.0.2125.111 safari/537.36', 'lang': 'en_in', 'language': 'en_in:en', 'ld_library_path': '/home/ankita/tar_files/pycharm-3.4.1/bin:', 'logname': 'ankita', 'mandatory_path': '/usr/share/gconf/ubuntu.mandatory.path', 'node_path': '/usr/lib/nodejs:/usr/lib/node_modules:/usr/share/javascript', 'path': '/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games', 'path_info': u'/catalog/products/5459c34218a3b30fa82ce983/edit/', 'pwd': '/home/ankita/nuvo_dev/peppertap/catalog_service', 'pycharm_django_manage_module': 'manage', 'pythonpath': '/home/ankita/nuvo_dev/peppertap/catalog_service', 'pythonunbuffered': '1', 'query_string': '', 'remote_addr': '127.0.0.1', 'remote_host': '', 'request_method': 'get', 'run_main': 'true', 'script_name': u'', 'server_name': 'localhost', 'server_port': '8000', 'server_protocol': 'http/1.1', 'server_software': 'wsgiserver/0.1 python/2.7.3', 'session_manager': 'local/ankita-300e5e-300e4e-300e5v-300e4v:@/tmp/.ice-unix/1941,unix/ankita-300e5e-300e4e-300e5v-300e4v:/tmp/.ice-unix/1941', 'shell': '/bin/bash', 'shlvl': '1', 'ssh_agent_pid': '1977', 'ssh_auth_sock': '/tmp/keyring-tx9ufr/ssh', 'tz': 'asia/kolkata', 'ubuntu_menuproxy': 'libappmenu.so', 'user': 'ankita', 'xauthority': '/home/ankita/.xauthority', 'xdg_config_dirs': '/etc/xdg/xdg-ubuntu:/etc/xdg', 'xdg_current_desktop': 'unity', 'xdg_data_dirs': '/usr/share/ubuntu:/usr/share/gnome:/usr/local/share/:/usr/share/', 'xdg_seat_path': '/org/freedesktop/displaymanager/seat0', 'xdg_session_cookie': '10ec76f99f8a073167a9d3230000000a-1415772321.260333-1476791199', 'xdg_session_path': '/org/freedesktop/displaymanager/session0', '_': '/home/ankita/nuvo_dev/peppertap/catalog_service/env/bin/python', 'wsgi.errors': <open file '<stderr>', mode 'w' @ 0x7fe134578270>, 'wsgi.file_wrapper': <class wsgiref.util.filewrapper @ 0x363b738>, 'wsgi.input': <socket._fileobject object @ 0x38b6ad0>, 'wsgi.multiprocess': false, 'wsgi.multithread': true, 'wsgi.run_once': false, 'wsgi.url_scheme': 'http', 'wsgi.version': (1, 0)}>
my logs in jquery script printing. ajax beingness sent nil happening in view.
i log in ajax's error portion :
post http://localhost:8000/catalog/products/5459c34218a3b30fa82ce983/edit/ 403 (forbidden)
i think it's because not sending csrf_token in ajax request.
jquery ajax django django-views
No comments:
Post a Comment