问题描述
在我的模型中存储评分被排序,同样在页面上显示分数。但是我希望用户能够对页面进行评估(从1到5),而不需要刷新或更改页面。
我已经找到以下内容了,这里的星星风格:
目前对javascript和AJAX的理解有限。有没有人知道如何使用上述示例中的星星与AJAX和Django结合使用,所以您可以在用户选择评级时更新数据库(模型),而不刷新页面?
同样重要的是,用户只能投票一次,即不允许对页面评分两次。它们存储在模型中,无论他们是否已经投票,以及之前的投票是什么。但是我可以如何修改星星来显示?
所以如果你知道如何做这些事情,或者更合适的星级图形解决方案,或任何好的教程...请分享。谢谢。
AJAX听起来很可怕,令人困惑,但不一定是。基本上你想做的是将一些数据发布到特定的url / view组合。有关使用AJAX向服务器发送数据的更多信息,请参见。
#urls
urlpatterns + = patterns('',
url(r'^ article / rate /', 'article.rate'),
#views
def rate(request):
if request.method =='POST':
#use post data to完成评级..
#javascript
$ .post(/ article / rate,{rating:3,article:2},
function(data){
//成功!所以现在将UI明星设置为3
});
据我所知,使用无线电控制和css制作星级,所以如果要在页面上显示当前每个用户的评分,只需让您的模板使用选中
选项。
I am trying to implement a star rating system on a Django site.
Storing the ratings in my models is sorted, as is displaying the score on the page. But I want the user's to be able to rate a page (from 1 to 5 essentially) without a refresh or change of page.
I have found the following, and like the style of the stars here: http://jvance.com/blog/2008/09/22/JQueryRaterPluginNew.xhtml
Currently have a limited understanding of javascript and AJAX. Does anyone know how to use the stars in the above example combined with AJAX and Django, so you are able to update the database (models) without a page refresh when a user selects a rating?
It is also important that users are only able to vote once, i.e. they are not allowed to rate a page twice. It is stored in the models whether they have already voted and what their previous vote was. But how would I be able to modify the stars to show this?
So if you know how to do these things, or a more appropriate star rating graphics solution, or any good tutorials... please do share. Thank you.
AJAX sounds scary and confusing but it doesn't have to be. Essentially what you want to do is post some data to a particular url/view combo. See jQuery.post for more information on using AJAX to send data to the server.
#urls
urlpatterns += patterns('',
url(r'^article/rate/', 'article.rate'),
#views
def rate(request):
if request.method == 'POST':
# use post data to complete the rating..
#javascript
$.post("/article/rate", { rating: 3, article: 2 },
function(data) {
// success! so now set the UI star to 3
});
As far as I know, star-ratings are produced with radio controls and css. So if you want to show the current rating per user on load of the page, just have your template render the associated radio with the checked
option.
这篇关于Django星级评定系统和AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!