如何使用jQuery在没有刷新页面的情况下在Django中发出get请求?

时间:2021-12-03 16:55:34

I have made a five star rating view with five buttons (no stars yet). It basically loads a page which does the rating and then comes back to the original page very quickly. How can I use jQuery to not refresh the original page but still make a call to the view and update my rating?

我用五个按钮(没有星星)制作了一个五星评级视图。它基本上加载了一个评级的页面,然后很快回到原始页面。如何使用jQuery不刷新原始页面,但仍然可以调用视图并更新我的评级?

views.py

views.py

def RateBusiness(request, pk, rate):
    business = Business.objects.get(id=pk)
    defaults = {'rating': rate}
    BusinessRating.objects.update_or_create(business=business, user=request.user, defaults=defaults)
    return HttpResponseRedirect('/business-detail/%s/' % pk)

Original page link : domain/business-detail/2/

原始页面链接:domain / business-detail / 2 /

Rating page link : domain/business-detail/2/rate/1/

评级页面链接:domain / business-detail / 2 / rate / 1 /

template

模板

<div class="btn-group">
  <a class="btn btn-sm btn-default" href="{% url 'rate-business' object.id 1 %}">1</a>
  <a class="btn btn-sm btn-default" href="{% url 'rate-business' object.id 2 %}">2</a>
  <a class="btn btn-sm btn-default" href="{% url 'rate-business' object.id 3 %}">3</a>
  <a class="btn btn-sm btn-default" href="{% url 'rate-business' object.id 4 %}">4</a>
  <a class="btn btn-sm btn-default" href="{% url 'rate-business' object.id 5 %}">5</a>
</div>

Although the question says without refreshing page, I do need to refresh the <p> tag which shows the current rating, so that it gets updated after someone rates. Thanks.

虽然问题是没有刷新页面,但我确实需要刷新显示当前评级的

标签,以便在有人评价后更新。谢谢。

1 个解决方案

#1


1  

Use Ajax. It is really simple using it with jQuery.

使用Ajax。使用jQuery非常简单。

It will create async request on provided URL, in a success part you can change your value in <p> tag to show the average of rating.

它将在提供的URL上创建异步请求,在成功部分,您可以在

标记中更改您的值以显示评级的平均值。

Edited based on comment: In your HTML, create <p> tag something like this

根据评论进行编辑:在您的HTML中,创建

标记

<p id="avg_rating"></p>

And your Ajax call shoud be:

你的Ajax调用应该是:

    $.ajax({
    url:'{% your_url %}',
    success: function (response) {
        console.log(response); //response is value which you return from Django view
        $("#avg_rating").text(response); //this statment will fill `<p id="avg_rating">` with value you return from Django view
    }
});

#1


1  

Use Ajax. It is really simple using it with jQuery.

使用Ajax。使用jQuery非常简单。

It will create async request on provided URL, in a success part you can change your value in <p> tag to show the average of rating.

它将在提供的URL上创建异步请求,在成功部分,您可以在

标记中更改您的值以显示评级的平均值。

Edited based on comment: In your HTML, create <p> tag something like this

根据评论进行编辑:在您的HTML中,创建

标记

<p id="avg_rating"></p>

And your Ajax call shoud be:

你的Ajax调用应该是:

    $.ajax({
    url:'{% your_url %}',
    success: function (response) {
        console.log(response); //response is value which you return from Django view
        $("#avg_rating").text(response); //this statment will fill `<p id="avg_rating">` with value you return from Django view
    }
});