我要與大家分享我的喜歡按鈕應用體驗
首先創建像應用程序和like.models內建立
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class LikeModel(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, default=1)
liked = models.BooleanField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
timestamp = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return str(self.user.username)
那麼你應該有一個AJAX應用程序,我們將執行save co mmands只需點擊一下鼠標對心臟或拇指或任何你想要的,一旦你創建AJAX應用程序,然後不改變任何車型只是adjsut網址,並獲得ajax.views內,建立規範
def like_it(request):
user = request.user
if request.method == 'POST':
ObjectId = int(request.POST['objectid'])
Tip = str(request.POST['contentType'])
likes = LikeModel.objects.filter(object_id=ObjectId) # in here we filtered the particular post with its id
if likes: # if the particular post is there
if str(user) in str(likes): # then we check the user which is us, in there
like_obj = LikeModel.objects.get(user=user,object_id=ObjectId) #if we there and we returned this data, this part for saving data, I mean if this data is already created than we dont have to delete and create again, we just change LikeModel.liked true or false state, so that if you create like and it will never delete, it just change liked or like state
else:
pass
if Tip == 'UserPost':
post_content_type_by = UserPost.objects.all().first()
if str(user) not in str(likes):
like = LikeModel.objects.create(user=user,liked=True,content_type=post_content_type_by.get_content_type,object_id=ObjectId)
like.save() # if data is created then we say 'new'
okey = 'new'
elif str(user) in str(likes) and like_obj.liked:
like_obj.liked = False
like_obj.save() # if data is already there, then we save it False
okey = 'false'
elif str(user) in str(likes) and like_obj.liked == False:
like_obj.liked = True
like_obj.save() # if data is already changed to False and we save again to True
okey = 'true'
return render(request,'ajaxlike.html',{'likes':likes,'okey':okey})
和之後我們將創建用於返回和保存ajax數據的ajax模板,我只是這麼稱呼它。HTML
{% if okey == 'new' %}
new
{% elif okey == 'false' %}
false
{% elif okey == 'true' %}
true
{% endif %}
現在創建索引的HTML或任何你想要建立像按鈕,並寫了jQuery代碼
$('.thumb').click(function(){
var indx = $('.thumb').index(this)
var ObjectId = $('.ObjectId:eq('+indx+')').text()
$.ajax({
type: 'POST',
url: '/ajax/ajaxlike/',
data: {
'contentType':'UserPost',
'objectid':ObjectId,
'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val(),
},
success: LikePost,
dataType: 'html'
});
function LikePost(data, textStatus, jqXHR){
if($.trim(data) == 'new'){
$('.thumb:eq('+indx+')').css('color','#FF0033');
}else if($.trim(data) == 'false'){
$('.thumb:eq('+indx+')').css('color','');
}else if($.trim(data) == 'true'){
$('.thumb:eq('+indx+')').css('color','#FF0033');
}
}
});
上面的例子中,當我們點擊拇指並且它將數據保存在LikeModel中,然後從like.html中返回ajax數據,如果數據是新的並且它將大拇指上色爲紅色,如果數據爲假,意味着該數據已經保存但現在你想刪除像,所以然後拇指彩色回到正常的顏色,如果數據是真實的,這意味着你已經創建了像這篇文章的數據,但是然後你刪除你的拇指,但現在你想再次喜歡拇指去紅了起來
好,這是幾乎完成,但請記住,當頁面刷新所有顏色的大拇指,並計算在頁面未顯示likers,所以它是如此簡單只是寫一些小的代碼,它會重新加載一切
所以我們在這裏所做的,我們創建類似於應用程序,並在數據庫的類似表格中創建數據之後,用戶創建數據後永遠不會被用戶刪除,只需通過布爾值將其更改爲喜歡的真或假狀態因此,作爲一個admi ñ你總是會保留所有可能對我們有用的數據,但棘手的部分是,所有的事件都是由JavaScript處理的,所以你必須注意用jQuery編寫代碼時,例如確保Ajax運行良好,而且我也想提及這一點,如果在你的頁面例如首先你只加載10個帖子,然後滾動頁面和頁面自動加載其他10個帖子,像那樣的按鈕不會工作,因爲你的主要jQuery代碼在頁面不會工作在加載的div,所以我的意思是你必須做一些額外的事情,
但主要的想法,我想分享我的自定義喜歡和ajax應用程序,它爲我工作,這一個永遠不會失敗版本爲我:D,真誠
這是完美的,如果他想保持喜歡日誌。如果沒有,我會想''Picture'模型中的一個簡單的'number_of_likes'字段。 – 2013-03-14 11:36:35
那麼函數每次會增加1? – donkeyboy72 2013-03-14 11:37:45
謝謝你的幫助 – donkeyboy72 2013-03-14 11:51:31