1

How can I send data ( the user_to_follow in this example ) to my views.py function ( follow ) to do some updates?

I'm trying to send the username from my JavaScript and then add the logged-in username to that username following list ( all done in my views.py function )

Views.py.

def follow(request, profile_to_follow):
    try:
        user_to_follow = User.objects.get(username=profile_to_follow)
        user_to_following = Profile.objects.get(user=request.user.id)
    except User.DoesNotExist:
        return JsonResponse({"error": "Profile not found."}, status=404)
    if request.method == "PUT":
        user_to_following.following.add(user_to_follow)
        return HttpResponse(status=204)

index.js

function follow_user(user_to_follow){
    // How To send the user_to_follow to my views.py method.
    // to add the current logged in user to the user_to_follow following list
    console.log(user_to_follow)
}

urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("", views.index, name="index"),
    path("login", views.login_view, name="login"),
    path("logout", views.logout_view, name="logout"),
    path("register", views.register, name="register"),

    # API Routes
    path("posts", views.compose_post, name="compose_post"),
    path("posts/all_posts", views.show_posts, name="show_posts"),
    path("posts/<int:post_id>", views.post, name="post"),
    path("profile/<str:profile>", views.display_profile, name="profile"),
    path("<str:profile_posts>/posts", views.display_profile_posts, name="profile_posts"),
    path("follow/<str:user_to_follow>", views.follow, name="follow"),
]
2
  • If you found my answer as useful and correct can you mark it as accepted? Commented May 3, 2021 at 13:56
  • Yes sir, thanks! I haven't yet because I am still learning it. Commented May 3, 2021 at 23:46

1 Answer 1

2

The easiest method would be using an Ajax Query to send data from templates to your views.py

index.js

<script>
   $(document).ready(function() {
     function follow_user(user_to_follow){
     $.ajax({
         type: "PUT",
         url: "{% url 'follow' user_to_follow %}",
         data: {
                user_data: user_to_follow,
                csrfmiddlewaretoken: '{{ csrf_token }}'
                },
         success: function( data )
         {
         alert("Successful Added User to list");
         }
     });
     });
 });
</script>

views.py

def follow(request, profile_to_follow):
    try:
        user_to_follow = User.objects.get(username=profile_to_follow)
        user_to_following = Profile.objects.get(user=request.user.id)
    except User.DoesNotExist:
        return JsonResponse({"error": "Profile not found."}, status=404)
    if request.method == "PUT" and request.is_ajax():
        user_to_following.following.add(user_to_follow)
        return HttpResponse(status=204)

request.is_ajax()

This will return a boolean value based on whether the request is an AJAX call or not.

You can also refer to this documentation https://api.jquery.com/jQuery.ajax/#options

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.