使用Django和Ajax动态更新无法正常工作的信息

时间:2017-11-11 17:20:57

标签: python django

当我有一个多对多字段,我想动态更改(即,从视图更新数据库而不刷新整个页面),例如当用户点击“关注”时#39 ;对象的按钮(因为许多不同的用户可以跟随许多不同的对象)然后Ajax将发出请求并且数据库将被更新。 但是,这个Ajax调用似乎不适用于动态更改任何给定对象的布尔属性(将某些内容更改为True或False)。

这个基于类的视图可以动态添加或删除用户:

if user.userprofile in obj.followers.all():
    following = False
    obj.followers.remove(user.userprofile)
else:
    following = True
    obj.followers.add(user.userprofile)
updated = True

但是,此代码无法将属性更改为True或False:

if user.is_authenticated():
    if user.userprofile.content_pref == True:
        display = False
        user.userprofile.content_pref = False
    else:
        display = True
        user.userprofile.content_pref = True
updated = True

我怎么能达到这个效果?通过单击按钮,我希望用户将其模型对象属性从False切换为True或反之亦然...谢谢

0 个答案:

没有答案