简体   繁体   English

“POST /…………/…………/HTTP/1.1” 405 0 - Django

[英]"POST /......... / ......... / HTTP/1.1" 405 0 - Django

I have a problem, in my work I need to insert a system of likes to published posts but by clicking on the button that should give the post a like, nothing happens and this error comes out..."POST /posts/like/ HTTP/ 1.1" 405 0我有一个问题,在我的工作中,我需要在已发布的帖子中插入一个点赞系统,但是通过单击应该给帖子点赞的按钮,没有任何反应并且出现了这个错误......“POST /posts/like/ HTTP/ 1.1" 405 0

views.py视图.py

@ajax_required
@require_POST
@login_required
def post_like(request):
    post_id = request.POST.get('id')
    action = request.POST.get('action')

    if post_id and action:
        try:
            post = Post.objects.get(id=post_id)
            if action == 'like':
                post.users_likes.add(request.user)

            else:
                post.users_likes.remove(request.user)

            return JsonResponse({'status': 'ok'})

        except:
            pass

    return JsonResponse({'status': 'error'})

urls.py网址.py

from django.urls import path, include
from . import views


app_name = 'posts'

urlpatterns = [
    path('like/', views.post_like, name='like'),
]

index.html index.html

        {% with total_likes=post.users_likes.count users_likes=post.users_likes.all %}

            <div class="post-info">
                <div>
                    <span class="count">
                        <span class="total">{{ total_likes }}</span>
                        like{{ total_likes|pluralize }}
                    </span>

                    <a href="#" data-id="{{ post.id }}" data-action="{% if request.user in users_likes %}un{% endif %}like" class="like">
                        {% if request.user not in users_likes %}
                            Like
                        {% else %}
                            Unlike
                        {% endif %}
                    </a>
                </div>
            </div>

            <div class="post-likes">
                {% for user in users_likes %}
                    <div>
                        <p>{{ user.first_name }}</p>
                    </div>
                {% empty %}
                    <p>No body likes this post yet</p>
                {% endfor %}
            </div>
        {% endwith %}

        <p>{{ post.id }}</p>
        <a class="normal-text" href="{{ post.get_absolute_url }}">Discover more...</a>
    </div>
</div>

ajax code ajax 代码

<script>
    {% block domready %}

        $('a.like').click(function(e){
            e.preventDefault();
            $.post("{% url 'posts:like' %}",
                {
                    id: $(this).data('id'),
                    action: $(this).data('action')
                },
                function(data){
                    if (data['status'] == 'ok')
                    {
                        var previous_action = $('a.like').data('action');

                        // toggle data-action
                        $('a.like').data('action',
                            previous_action == 'like' ? 'unlike' : 'like');

                        // toggle link-text
                        $('a.like').text(
                            previous_action == 'like' ? 'Unlike' : 'Like');

                        // update total likes
                        var previous_likes = parseInt(
                            $('span.count .total').text());
                        $('span.count .total').text(previous_action == 'like' ? previous_likes + 1 : previous_likes -1);
                    }
                }
            );
        });

    {% endblock %}
</script>

I don't understand where the problem is... I thought in the url but I don't think so我不明白问题出在哪里...我认为是 url 但我不这么认为

From the documentation , it appears that your urls.py is at fault - you define a "like/" pattern, but there's nothing to tell the router that you mean for it to have a "posts/" prefix.文档来看,您的urls.py似乎有问题 - 您定义了一个“like/”模式,但是没有什么可以告诉路由器您的意思是它有一个“posts/”前缀。

The examples given there explicitly use an include directive to do that, and you do no such thing.此处给出的示例明确使用include指令来执行此操作,而您不会这样做。 Try:尝试:

from django.urls import include, path
from . import views

urlpatterns = [
    path('posts/', include([
        path('like/', views.post_like, name='like'),
        # Other posts/... endpoints
    ])),
    # path('users/', include([ ...
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM