Django AJAX链接未进入相应视图

Django AJAX链接未进入相应视图,ajax,django,django-templates,django-views,Ajax,Django,Django Templates,Django Views,我希望用户能够在公开列表中隐藏他们的帖子,或者让它再次可见。我的想法是,只要有一个链接,点击它就会改变文本,也会改变帖子的状态。现在文本发生了变化,但帖子的状态没有变化,“我在这里”也没有打印出来 这是一种观点: @login_required def change_status(request): print("I'm here") if request.method == 'GET': post_id = request.GET['post_id']

我希望用户能够在公开列表中隐藏他们的帖子,或者让它再次可见。我的想法是,只要有一个链接,点击它就会改变文本,也会改变帖子的状态。现在文本发生了变化,但帖子的状态没有变化,“我在这里”也没有打印出来

这是一种观点:

@login_required
def change_status(request):
    print("I'm here")
    if request.method == 'GET':
        post_id = request.GET['post_id']
        post = Post.objects.get(pk=post_id)
        if request.user.profile.post == post:
            if post.status == 'published':
                post.status = 'hidden'
            else:
                post.status = 'published'
            post.save()
            return HttpResponse("Success!")
        else:
            return HttpResponse("Request not from author")
    else:
        return HttpResponse("Request method is not a GET")
网址:

url(r'^status/$', views.change_status, name='change_status'),
模板:

{% extends "base_generic.html" %}

{% block content %}

<h1>{{ post.title }}</h1>
<p> {{ post.author }} - {{ post.pub_date }}</p>

{% for tag in post.tags.all %}
    <a href="{% url 'fortykwords:tag' tag.name %}">{{ tag.name }}</a>
{% endfor %}

<p>{{ post.body }}</p>
{% if post.author != request.user %}
<a href="{% url "pinax_messages:message_user_create" user_id=post.author.id %}" class="btn btn-default">Message this user</a>
{% endif %}
{% if post.author == request.user %}
    {% if post.status == 'published' %}
    <a class="likebutton" id="like{{post.id}}" href="#" data-catid="{{ post.id }}">Hide post</a>
    {% endif %}
    {% if post.status == 'hidden' %}
    <a class="likebutton" id="like{{post.id}}" href="#" data-catid="{{ post.id }}">Make post public again</a>
    {% endif %}
{% endif %}

<script type="text/javascript">
    $('.likebutton').click(function(){
    var catid;
    catid = $(this).attr("data-catid");
    $.ajax(
    {
        type:"GET",
        url: "/status",
        data:{
                 post_id: catid
        },
        success: function( data ) 
        {
            $( '#like'+ catid ).text("changed");
            $( '#message' ).text(data);
        }
     })
});
</script>
{% endblock %}
文本根据
$('#like'+catid)进行更改所以jquery加载正确,但由于它没有打印“我在这里”,我假设它没有进入视图。但是GET请求确实发生了:
[21/Aug/2018 16:46:59]“GET/status?post_id=10 HTTP/1.1”302 0
。你知道为什么这不起作用吗


提前感谢。

首先,我将其更改为POST请求,而不是GET请求,因为我们毕竟正在更改数据

<script type="text/javascript">
    $('.likebutton').click(function(){
    var catid;
    catid = $(this).attr("data-catid");
    $.ajax(
    {
        type:"POST",
        url: "/status/",
        data:{
                csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
                post_id: catid
        },
        success: function( data ) 
        {
            $( '#like'+ catid ).text("changed");
            $( '#message' ).text(data);
        },
        error: function(xhr, textStatus, errorThrown) {
            alert("Please report this error: "+errorThrown+xhr.status+xhr.responseText);
        }
     })
});

现在它工作正常。

为什么不在模型中使用标志
hidden=models.BooleanField(default=False)
然后您可以在模板中添加一个单选按钮,如果您愿意,您可以使用ajax修改其状态。使用选择变量和布尔值之间有什么重要区别吗?我可以用这个视图的单选按钮吗?在这种情况下,旗帜会更干净。你可以在任何一种情况下使用单选按钮。我应该在url中使用什么?帖子的状态是否正在更改?您是否收到任何http响应数据?您是否已登录?请注意,在请求正文中发送CSRF令牌不是一种好的做法。应将其添加到请求的标头()中
<script type="text/javascript">
    $('.likebutton').click(function(){
    var catid;
    catid = $(this).attr("data-catid");
    $.ajax(
    {
        type:"POST",
        url: "/status/",
        data:{
                csrfmiddlewaretoken: document.getElementsByName('csrfmiddlewaretoken')[0].value,
                post_id: catid
        },
        success: function( data ) 
        {
            $( '#like'+ catid ).text("changed");
            $( '#message' ).text(data);
        },
        error: function(xhr, textStatus, errorThrown) {
            alert("Please report this error: "+errorThrown+xhr.status+xhr.responseText);
        }
     })
});
@login_required
def change_status(request):
    print("I'm here")
    if request.is_ajax():
        post_id = request.POST['post_id']
        post = Post.objects.get(pk=post_id)
        if request.user.profile.post == post:
            if post.status == 'published':
                post.status = 'hidden'
            else:
                post.status = 'published'
            post.save()
            return HttpResponse("Success!")
        else:
            return HttpResponse("Request not from author")
    else:
        raise Http404