1

I am using Python 3.7.4 with Django 3.0.3 and I have a script Ajax in javascript run in the front end app. When the user click in link, a variable must to be sending to back end python. See the exemple

Javascript

$('.likebutton').click(function() {
    var catid;
    catid = $(this).attr("data-catid");
    $.ajax({
        type: "GET",
        // url: "/likePost",
        url: "/likePost/" + catid,
        /* data: {
            post_id: catid
        },
        */   
        success: function(data) {
            $('#like' + catid).remove();
            $('#message').text(data);
        }
    })
});

urls.py

In the urlpattner of app I have

urlpatterns = [
    path('', views.index, name='index'),  # index view at /
    path('likePost/', views.likePost, name='likepost'),   # likepost view at /likepost
]

views.py

def likePost(request):
    if request.method == 'GET':
        post_id = request.GET['post_id']
        likedpost = Post.obejcts.get(pk=post_id) #getting the liked posts
        m = Like(post=likedpost) # Creating Like Object
        m.save()  # saving it to store in database
        return HttpResponse("Success!") # Sending an success response
    else:
        return HttpResponse("Request method is not a GET")

In Debug I received the follow message error

Not Found: /likePost
[25/Feb/2020 16:12:17] "GET /likePost?post_id=1 HTTP/1.1" 404 2335

What I am doing wrong?

  • You are mixing up the two solutions I provided. You are now passing `post_id` as a **path parameter**, but are expecting it to come to your view as a **querystring** variable. Please read carefully, and if it still doesn't make sense, let me know and I will try to elaborate. – Lord Elrond Feb 25 '20 at 20:09
  • Thanks for help me @ReinstateMonica, but the error persists. I tried 2 options that you give me. but no sucess – Rafael Christófano Feb 25 '20 at 20:17
  • Ok, It's right now. I used post_id as a querystring. Thanks – Rafael Christófano Feb 25 '20 at 20:41

1 Answers1

2

In your ajax script, you are passing a querystring parameter called post_id (eg. likePost/?post_id=1), but in your urlpatterns, you specify post_id as a path parameter (eg. likePost/1/).

You have 2 options:

post_id as a path parameter

Add the post_id to the url instead of sending it as data:

$('.likebutton').click(function() {
    var catid;
    catid = $(this).attr("data-catid");
    $.ajax({
        type: "GET",

        // standard syntax
        url: "/likePost/" + catid,

        // template string syntax
        // url: `/likePost/${catid}`,

        success: function(data) {
            $('#like' + catid).remove();
            $('#message').text(data);
        }
    })
});

Then add post_id to your view:

def likePost(request, post_id):
    ...

post_id as a querystring

change your path to the following:

path('likePost/', views.likePost, name='likepost') 

You can then access post_id via request.GET in your view:

def likePost(request):
    post_id = request.GET['post_id']
    ...

Furthermore, I'd recommend reading When do I use path parameters vs. query params in a RESTful API? if you aren't sure of which option to use.

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80