如何在没有刷新页面的情况下调用django views.py函数?

时间:2019-06-22 12:21:25

标签: django

我编写了一个模型,用户可以彼此关注/取消关注,但是在该过程之后,用户单击按钮以关注或取消关注时,页面将刷新。我需要一种无需刷新页面即可更改该页面中文本的方法来处理该操作。

我的模特:

SET "InputFile=abc.txt"
IF EXIST "%InputFile%" (DEL /f /q "%InputFile%")
SETLOCAL EnableExtensions EnableDelayedExpansion
FOR /F Tokens^=* %%G IN ('
    "WMIC PATH Win32_DiskPartition WHERE ^(DeviceID^="Disk #0, Partition #1"^) Assoc:list /AssocClass:Win32_LogicalDiskToPartition 2>NUL"

') DO (CALL ECHO %%G>> "%InputFile%")

ENDLOCAL

我的观点:

class Follower(models.Model):
  follower = models.ForeignKey(User, related_name='followings',on_delete=models.CASCADE)
  following = models.ForeignKey(User, related_name='followers',on_delete=models.CASCADE)

  class Meta:
    unique_together = ('follower', 'following')

  def __str__(self):
    return u'%s follows %s' % (self.follower, self.following)

我的模板:

def follow(request, own_user_id , follow_state):
  """
  follow function
  that add user who follow other user
  own : user that have channel page
  """
  own = CustomUser.objects.get(pk=own_user_id)

  if follow_state == "followed":
     action = request.user.followings.get(following=own)
     action.delete()
     return redirect(f'http://127.0.0.1:8000/{own.channel_name}')
 else:
     action = Follower.objects.create(follower=request.user, following=own)
     action.save()
     return redirect(f'http://127.0.0.1:8000/{own.channel_name}')

0 个答案:

没有答案