gpt4 book ai didi

django - 最佳实践 : How to best implement Rating-Stars in Django Templates

转载 作者:行者123 更新时间:2023-12-04 01:08:47 30 4
gpt4 key购买 nike

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center为指导。




9年前关闭。




我想要可重复使用的评级(5 星的典型布局)。
我找到了这个 http://www.thebroth.com/blog/119/css-rating-stars这解释了如何使用 css 显示它。
为了实际收集评级,我正在考虑使用图像 map 或简单的单选按钮。
我想在各种不同的模型上使用它。

你会怎么做?
我应该创建一个小部件还是可以使用模板来完成?
实际上,我很惊讶在网上找不到任何关于此的内容。是这么简单,还是不常见?

最佳答案

如果在 django-users mailing list 上收到一些有趣的答案:

迈克:

Well you can create a widget, I like a seperate rating model myself. That collects the value and then adds that to a total and creates a score or average. The model stores the total votes and the total score, which I divide and get my average, (I do the math in the view). Adding it to other models with a foreign key relation. Enforcing that users vote only once is rarely enforced outside of the current session or cookie lifetime. If you want it persistance, I'm notfgv6gw33TT sure off the top of my head what is best for this, but would require only registered users vote. Now, you just display the rating form, I would do it as a template inclusion tag and put the tag in my templates. This tag has the basic submit form, the form it's self is two fields, with a select box (I went simple this way) and a hidden field labeled next that points back to this page, that I can redirect to. When the user submits, in my views to handle the forms action, I just increment the votes and total score and redirect back to the page the vote was taken on. This is using the traditional submit button, posting the form to a url, returning a full view. If you do something with javascript that illuminates the number of stars for the rating and click on the stars to submit, here you might want to post it as json object using xhr request, update the view and return a json object with the updated rating values, if it's a 200, update the page with the new values after voting (returned with the 200). If it's a 500, deal with the error, letting the user know, there was a problem voting and reset the stars. This is what I do, or would do in your position, if anyone has a better idea, please speak up. Hope this helps. Mike



通过伊森:

I actually just did 5-star ratings for a project I'm working on, and have been trying to figure out if I have anything reusable worth releasing as a package (and trying to find the time to figure that out..) I'll outline what I did and what I used to do it. I used django-ratings[1,2] for the backend and hooked up its RatingField to my rateable models. I like jQuery, so for the frontend I used the jquery-star-rating plugin[3,4] as a base. It turns a collection of radio buttons into a star widget. I haven't looked closely at the implementation but I think it's basically using the same CSS technique described in your link. To get started you just need to include its JS and CSS and add class="star" to the radio buttons in your form. I then just wrote some view code that sends the request data from the radio buttons to django-ratings. Super simple stuff, just used the django-ratings RatingManager API and handled the exceptions it throws -- I've pasted the snippet from my code at [5]. (I'm using a somewhat old version of django-ratings b/c I haven't had the time to upgrade; it might look a little different now, I'm not sure.) Finally, I wanted two more things: 1) If a user has already rated an item and views the "rate this item" form again, the "star widget" should be preset with the user's previous rating, instead of just showing five blank stars. I realized the easiest way to do this was from the client side: an onload event that simulates the user clicking on the star he already clicked on. My view and template code for that is at [6]; I just figured out the HTML formats that jquery-star-rating sets and expects, and clicked on the appropriate star for the user's existing rating. 2) When viewing the item, users' ratings should show up as non-interactive stars, instead of as numbers. I wrote a dumb-as-nails template filter designed to take a number (the rating) and return a bunch of star images. Again, I just used the HTML formatting and CSS classes from jquery-star-rating. My code for this is at [7]. I was thinking it'd be neat to put some of this in a django-form Field that renders the radio buttons and triggers jquery-star-rating all in one go, and handles the submission to the django-ratings backend. But I haven't had a chance to figure that out yet. Anyway, hope this helps, Ethan 1 http://github.com/dcramer/django-ratings [2] http://pypi.python.org/pypi/django-ratings [3] http://www.fyneworks.com/jquery/star-rating/ [4] http://code.google.com/p/jquery-star-rating-plugin/ [5] http://pastebin.ca/1650596 [6] http://pastebin.ca/1650609 [7] http://pastebin.ca/1650616

关于django - 最佳实践 : How to best implement Rating-Stars in Django Templates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1649632/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com