Django Up / Down投票如何制作模板?

时间:2013-11-28 21:35:51

标签: django voting

Django noob在这里。我已经安装了updown package并按照说明将其添加到我的UserPic模型上。以下是相关部分:

class UserPic(models.Model):
    user = models.ForeignKey(User, unique=False)
    picfile = models.FileField(upload_to=get_uplaod_file_name)
    created_at = models.DateTimeField(auto_now_add=True)
    rating = RatingField(can_change_vote=True)

urls.py

urlpatterns = patterns('',  
    url(r"^(?P<object_id>\d+)/rate/(?P<score>[\d\-]+)$", AddRatingFromModel(), {
            'app_label': 'userpic',
            'model': 'UserPic',
            'field_name': 'rating',
        }, name="pic_rating"),
    rl(r'^(?P<pic_id>\d+)/$', 'userpics.views.photo'),  
    url(r'^(?P<pic_id>\d+)/delete$', 'userpics.views.del_photo'),
    url(r'^/$', 'userpics.views.list'),

views.py

def photo(request, pic_id):
    if request.method == 'GET':
        pic = UserPic.objects.get(pk=pic_id)
        hasNext=False
        hasPrev=False
        picOwner=False

        next = int(pic_id) + 1
        prev= int(pic_id) - 1

        if pic.user== request.user:
            picOwner= True
        try:
            if UserPic.objects.get(pk=next):
                hasNext =True
        except:
            pass
        try:
            if UserPic.objects.get(pk=prev):
                hasPrev = True 
        except:
            pass

        message = pic_id
        return render_to_response('userpics/photo.html',
                                      {'pic':pic, 'hasNext':hasNext, 'hasPrev':hasPrev, 'picOwner':picOwner},
                                      context_instance=RequestContext(request))

photo.html

{% if pic %}

<form method="post" action="/photo/{{pic.id}}/rate/1" >
{% csrf_token %}
<input type="button" value="Up">
</form>

<form method="post" action="/photo/{{pic.id}}/rate/-1" >
{% csrf_token %}
<input type="button" value="Down">
</form>

Rating {{pic.rating}}
 <img class="pic" src="/static/assets/{{pic}}" />

但是,创建的按钮似乎不起作用。代替{{pic.rating}},我得到的结论是:

Rating <updown.fields.RatingManager object at 0x7f5c64404990> 

不幸的是,文档不是万无一失的,所以我感谢您帮助实现该应用程序。

1 个答案:

答案 0 :(得分:0)

看起来它看起来不像django-updown甚至暴露了一个“总”评级,只有upvotes和downvotes的数量。你可以通过以下方式获得这些:

{{ pic.rating.likes }} and {{ pic.rating.dislikes }}