Giter Site home page Giter Site logo

Comments (2)

epicserve avatar epicserve commented on July 17, 2024

@iranana,

I'm learning django-relationships and saw your question. I tested the following and it worked for me.

{% extends "base.html" %}

{% load url from future %}

{% block bottom_script %}
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $.ajaxSetup({
                 beforeSend: function(xhr, settings) {
                     function getCookie(name) {
                         var cookieValue = null;
                         if (document.cookie && document.cookie != '') {
                             var cookies = document.cookie.split(';');
                             for (var i = 0; i < cookies.length; i++) {
                                 var cookie = jQuery.trim(cookies[i]);
                                 // Does this cookie string begin with the name we want?
                             if (cookie.substring(0, name.length + 1) == (name + '=')) {
                                 cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                                 break;
                             }
                         }
                     }
                     return cookieValue;
                     }
                     if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
                         // Only send the token to relative URLs i.e. locally.
                         xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
                     }
                 }
            });
            $('#add-friend-btn').click(function() {
                var add_link = $(this);
                $.post(add_link.attr('href'), {}, function(data) {
                    if (data.result === '1') {
                        alert("Success, you're now following your friend.");
                    } else {
                        alert("Something went wrong!");
                    }
                });
                return false;
            });
        });
    </script>
{% endblock bottom_script %}

{% block base_content %}
    <a href="{% url 'relationship_add' 'example-username' 'following' %}" id="add-friend-btn">Follow Friend</a>
{% endblock base_content %}

from django-relationships.

coleifer avatar coleifer commented on July 17, 2024

Yes -- and depending on your middleware etc you may not need the csrftoken logic.

from django-relationships.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.