Do you mean that this line should be changed?
No ! Don't Print it
On Tuesday, May 19, 2020 at 11:11:12 PM UTC-4, Hella Nick wrote:
>
> button标签中的type属性设置为button,
>
> Ahmed Khairy > 于2020年5月20日周三 上午1:54写道:
>
>> I am trying to use Ajax to submit a like button, I believe everything is
>> in order but I keep getting django.urls.exceptions.NoReverseMatch: Reverse
>> for 'like_post' with arguments '('',)' not found. 1 pattern(s) tried:
>> ['score/like/(?P[0-9]+)$']
>>
>>
>> I am not sure what is the reason. Need help to identify the error.
>>
>>
>> Here is the view
>>
>>
>> class PostDetailView(DetailView):
>> model = Post
>> template_name = "post_detail.html"
>>
>> def get_context_data(self, *args, **kwargs):
>> context = super(PostDetailView, self).get_context_data()
>> stuff = get_object_or_404(Post, id=self.kwargs['pk'])
>> total_likes = stuff.total_likes()
>> liked = False
>> if stuff.likes.filter(id=self.request.user.id).exists():
>> liked = True
>> context["total_likes"] = total_likes
>> context["liked"] = liked
>> return context
>>
>>
>> def LikeView(request, pk):
>> # post = get_object_or_404(Post, id=request.POST.get('post_id'))
>> post = get_object_or_404(Post, id=request.POST.get('id'))
>> like = False
>> if post.likes.filter(id=request.user.id).exists():
>> post.likes.remove(request.user)
>> like = False
>> else:
>> post.likes.add(request.user)
>> like = True
>> context["total_likes"] = total_likes
>> context["liked"] = liked
>>
>> if request.is_ajax:
>> html = render_to_string('like_section.html', context, request=
>> request)
>> return JsonResponse({'form': html})
>>
>> Here is the url.py updated
>>
>> urlpatterns = [
>> path('user/', UserPostListView.as_view(), name=
>> 'user-posts'),
>> path('', PostListView.as_view(), name='score'),
>> path('who_we_Are/', who_we_are, name='who_we_are'),
>> path('/', PostDetailView.as_view(), name='post-detail'),
>> path('like/', LikeView, name='like_post'),
>> path('new/', PostCreateView.as_view(), name='post-create'),
>> path('/update/', PostUpdateView.as_view(), name='post-update'
>> ),
>> path('/delete/', PostDeleteView.as_view(), name='post-delete'
>> )
>> ]
>>
>> here is the template
>>
>>
>> {% csrf_token %}
>> Likes: {{total_likes}}
>> {% if user.is_authenticated %}
>> {% if liked %}
>> > 'post_id' class= "btn btn-danger btn-sm"
>> value="{{post.id}}"> Unlike
>> {% else %}
>> > 'post_id' class= "btn btn-primary btn-sm"
>> value="{{post.id}}"> Like
>> {% endif %}
>> {% else %}
>> > > Login to Like
>> {% endif %}
>>
>>
>> here is the ajax
>>
>> https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
>> script>
>>
>>