SHARE
TWEET

Untitled

a guest Jun 24th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enter code here
  2. ##models.py
  3.  
  4. class Images(models.Model):
  5.     user = models.ForeignKey(settings.AUTH_USER_MODEL,
  6.               related_name='images_created', on_delete=models.CASCADE)
  7.     created = models.DateTimeField(auto_now_add=True, null=True,        
  8.                blank=True)
  9.     message_likes = models.IntegerField(default=0)
  10.     objects = InheritanceManager()
  11.  
  12. ##views.py
  13.  
  14. def like_treasure(request):
  15.     treasure_id = request.POST.get('treasure_id', None)
  16.     message_likes = 0
  17.     if (treasure_id):
  18.         treasure = Postsms.objects.get(id=int(treasure_id))
  19.         if treasure is not None:
  20.             message_likes = treasure.message_likes + 1
  21.             treasure.message_likes = message_likes
  22.             treasure.save()
  23.     return HttpResponse(message_likes)
  24.  
  25.  
  26. ## $
  27. $('button').on('click', function(event){
  28.     event.preventDefault();
  29.     var element = $(this);
  30.     $.ajax({
  31.     url : '/like_treasure/',
  32.     type : 'POST',
  33.     data : { treasure_id : element.attr("data-id")},
  34.     success : function(response){
  35.         element.html(' ' + response);
  36.  }
  37.  
  38. });
  39.  
  40. });
  41.  
  42. ##html
  43.  
  44.  <button style="background-color: red" id ="likes" data-id="{{foo.id}}"
  45.    class="btn btn-mini  glyphicon glyphicon-heart"
  46.    type="button" >
  47.    {% if foo.message_likes > 0 %} {{ foo.message_likes }} {% endif %}
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top