0

Solution: My mistake was that the url attribute doesn't just add the string given to 127.0.0.1 but to the current url, and so the url for the Like view was supposed to be 127.0.0.1/article/article_id/like-article-commm

I wrote a django app that has articles and im trying to add a like functionality, I added the code in the bottom and nothing happens. No error just nothing in the database or the html code changes. Any idea what is the problem?

The relevent part of the html/javascript code:

    <head>
            <script src="https://code.jquery.com/jquery-3.4.1.js" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" crossorigin="anonymous"></script>
        </head>

        <button id='like-button' color = 'black'> Like </button>

<script type="text/javascript"> 
$('#like-button').click(function(){ 
var article_id = '{{ article.id }}';
var user_id = '{{ user.id }}';
var like_dislike = True;

$.ajax( 
{ 
    type:"GET",
    url: "like-article-commm",
    data:{ 
             article_id: article_id,
             user_id: user_id,
             like_dislike: like_dislike
}, 
success: function( data ) 
{ 
    $('#like-button').css('color', 'blue'); } }) });
</script>

The like-article-comm View:
def Like_Article_View(request):
    if request.method == 'GET':
        article_id = int(request.GET['article_id'])
        likedarticle = Article.objects.get(id = article_id)
        
        user_liked_id = int(request.GET['user_id'])
        userliked = User.objects.get(id = user_liked_id)

        like_dislike_0 = request.GET['like_dislike']

        like_object_list = Like_Article.objects.filter(article_liked = likedarticle, user_liked = userliked)

        if like_object_list.count() > 0:
            existing_like = like_object_list.filter()
            if existing_like.like_dislike == like_dislike_0:
                return HttpResponse('success')
            existing_like.like_dislike = like_dislike_0
            existing_like.save()




        like_new_object= Like_Article(article_liked=likedarticle, user_liked=userliked, like_dislike=like_dislike_0)
        like_new_object.save()

        return HttpResponse('success')
    else:
        return HttpResponse("unsuccesful")

urls.py file:

from django.urls import path
from . import views
from .views import ArticleListView, ArticleDetailView, ArticleCreateView, ArticleUpdateView, ArticleDeleteView

urlpatterns = [
    path('', ArticleListView.as_view(), name="home-comm"),
    path('article/<int:pk>/', ArticleDetailView.as_view(), name="article-comm"),
    path('like_article/', views.Like_Article_View, name='like-article-commm'),
]

I can add like objects to the database manually.

1 Answers1

0

--update--
After some discussion, we figure out that frontend ajax is not communicating with the backend properly. With chrome devtools we managed to pin point where the issue lies.

have you enabled CORS cross site resource sharing?

  • I dont know what that is, so probably not. How to check if it is enabeld and how to enabel it? – bookim online Aug 11 '21 at 11:38
  • sorry i am not able to make clarification under your post thus i answer your question with another question. back to your question i think you can refer to this question [link](https://stackoverflow.com/questions/35760943/how-can-i-enable-cors-on-django-rest-framework) as a starter – mushcatshiro Aug 11 '21 at 11:40
  • to put it in simple terms, django is serving at port say, 5000 (i work with flask another python web framework so not really sure about django), but your browser making async request from a different port / origin, it could be a malicious request to your server, so implicitly almost every web framework disabled CORS. – mushcatshiro Aug 11 '21 at 11:43
  • I added CORS however it didn't solve the problem – bookim online Aug 11 '21 at 11:47
  • can you show me your console log? you can access it from chrome dev tools. – mushcatshiro Aug 11 '21 at 11:51
  • also i think you should use a post request instead as you are making changes to the backend. – mushcatshiro Aug 11 '21 at 11:52
  • your ajax address should be 127.0.0.1:5000/like-article-commm or localhost:5000/like-article-commm instead – mushcatshiro Aug 11 '21 at 11:54
  • jquery-3.4.1.js:9837 GET http://127.0.0.1:8000/article/3/like-article-commm?article_id=7&user_id=2 404 (Not Found) – bookim online Aug 11 '21 at 12:02
  • This is the console log when the button is pressed – bookim online Aug 11 '21 at 12:03
  • shouldnt it be posted to the "like_article" endpoint instead? – mushcatshiro Aug 11 '21 at 12:05
  • when i first bring up CORS i am assuming your frontend ajax is able to communicate with your backend django. however right now your issues here is your frontend ajax is not able to communicate with the correct endpoint. – mushcatshiro Aug 11 '21 at 12:07
  • Thanks it finally worked, following your idea of checking the consule log I managed to pinpoint the - insane amount of - problems – bookim online Aug 11 '21 at 12:13
  • well i would appreciate if you accept my answer such that i can comment directly under people's question next time haha. – mushcatshiro Aug 11 '21 at 12:15
  • @mushcatshiro You should edit and update your answer with the actual solution. – rveerd Aug 11 '21 at 14:28