繁体   English   中英

使用 vanilla Javascript 在 Django 中将前端链接到后端

[英]Linking front-end to back-end in Django using vanilla Javascript

我了解您可以使用 fetch API 将数据从前端发送到后端,以由 django 视图处理。 我也知道您可以使用 jQuery API 来“获取数据”。

我想知道是否有任何其他常用方法仅使用 Javascript 从前到后发送数据,而不使用获取 API 或 jQuery7560EB7106CDBFDE71。

我在使用 URL 之前已经这样做了。

包括这样的东西:

    var binform = document.createElement('form');
    binform.setAttribute('method','POST');
    binform.setAttribute('action',`/unfollow/${data[i].crypto}/`);

在我的 javascript 中,从获取请求中获取并将该值传递给视图 function。

@csrf_exempt            
def deletecrypto(request, crypto):

        if request.method == "POST":
            user = request.user
            usercurr = User.objects.filter(username=user).first()
            userid = usercurr.id
            profilepic = ProfilePic.objects.filter(user=user).first()
            usr = request.user.username
            following = FollowedCrypto.objects.filter(user=user.id)
            cryptos = []
        
            for i in following:
                cryptos.append(i.cryptocurrency)

            if crypto in cryptos:
                newfollow = FollowedCrypto.objects.filter(user=user, 
                cryptocurrency=crypto).delete()
            
                return render(request,"PlanetCrypton/profile.html",{
                "following":following,
                "crypto": cryptos,
                "user": user,
                "usr" : userid,
            })
       

这就是我设法删除并在另一个 function 中向我的模型中添加加密货币的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM