【问题标题】:I can't make Django-ratings work我无法让 Django 评级工作
【发布时间】:2014-04-11 04:28:20
【问题描述】:

我正在使用 django 的这个应用程序:'Django-ratings',我已经使用它几天了,但我无法让它工作,我不知道我的代码有什么问题.

我在我的 INSTALLED_APPS 中添加了'djangoratings',然后我将字段rating = RatingField(range=5) 添加到了我想要评分的模型中。

这是我的看法

def votos(request, self):
if request.is_ajax():
    form = UbicacionForm(request.POST)
    if form.is_valid():
        u = form.save(commit=False)
        u.user = request.user
        u.save()
        form.save_m2m()
        Ubicacion.rating.add(score=self.rating, user=self.user, ip_address=self.ip_address)

        data = '<ul>'
        for ubicacion.rating in ubicaciones:
            data += '<li>%s %s s</li>' % (ubicacion.rating.score, ubicacion.rating.votes)
        data += '</ul>'

        return HttpResponse(simplejson.dumps({'ok': True, 'msg': data}), mimetype='application/json')
    else:
        return HttpResponse(simplejson.dumps({'ok':False, 'msg':'No fue valido!'}), mimetype='application/json')

我的模板:

{% 额定负载 %}

                {% csrf_token %}
                <td><input name="star" type="radio" class="star" value= "1"/></td> 
                <td><input name="star" type="radio" class="star" value= "2"/></td> 
                <td><input name="star" type="radio" class="star" value= "3"/></td> 
                <td><input name="star" type="radio" class="star" value= "4"/></td> 
                <td><input name="star" type="radio" class="star" value= "5"/></td> 

                {% rating_by_request request on ubicaciones.rating as vote %}
                {% if vote %}
                <td> {{ vote }}</td>
                {% else %}
                <td> Sorry, no one has voted yet!</td>
                {% endif %}`

我的ajax函数:

    $(".star").click(function(){
        var val = $(this).data("value");
        $.ajax({
            type: "POST",
            url : "votos",
            data : {
                "val" : val
            },
            success: function(data) {
                console.log(data);
            }
        });
    }); 

我的模型:

class UbicacionForm(ModelForm):

    class Meta:
       model = Ubicacion   

我的网址

          url(r'rate/(?P<object_id>\d*)/(?P<score>\d*)/', AddRatingFromModel(), {
                       'app_label': 'gmaps',
                       'model': 'ubicacion',
                       'field_name': 'rating',
                      }),

这个应用程序应该创建的新列,它在我要评分的模型内部创建,“rating_vote”和“rating_score”都在数据库中,但值为 0:/

【问题讨论】:

    标签: javascript python ajax django django-ratings


    【解决方案1】:

    目前在您的 votos 视图中,您正在使用 POST 数据填充 UbicacionForm。您发送的是单个值 val,这是您要添加的评分。

    此模型表单希望创建或加载 Ubicacion,但这永远不会有效,因为您只有一个值(我假设它实际上不是 Ubicacion 的一部分)。

    .

    此外,您还有第二个 url rate/(?P&lt;object_id&gt;\d*)/(?P&lt;score&gt;\d*)/ 未被使用。

    将您的 ajax 请求更改为指向 django-ratings 网址而不是 votos

    $(".star").click(function(){
        var val = $(this).data("value"),
    
            // Get the pk of the ubicacion from somewhere in your page
            ubicacion_pk = 1,
    
            // Build the url
            url = '/rate/' + ubicacion_pk + '/' + val + '/';
    
    
        $.ajax({
            type: "POST",
            url : url,
            success: function(data) {
                console.log(data);
            }
        });
    }); 
    

    【讨论】:

      猜你喜欢
      • 2023-03-12
      • 2016-12-05
      • 2013-05-09
      • 1970-01-01
      • 2016-05-03
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      相关资源
      最近更新 更多