简体   繁体   English

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

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

I understand that you can use the fetch API to send data from the front-end to your backend to be processed by django views.我了解您可以使用 fetch API 将数据从前端发送到后端,以由 django 视图处理。 I also know that you can use jQuery API to do the "fetching of data.我也知道您可以使用 jQuery API 来“获取数据”。

I am wondering if there are any other common methods using only Javascript to send data from the front to the back without using the fetch API or jQuery Ajax.我想知道是否有任何其他常用方法仅使用 Javascript 从前到后发送数据,而不使用获取 API 或 jQuery7560EB7106CDBFDE71。

I have done this before using URL.我在使用 URL 之前已经这样做了。

Included something like this:包括这样的东西:

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

in my javascript from a fetch request and passing that value to the view function.在我的 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,
            })
       

That's how I managed to delete and in another function, add to my models a cryptocurrency.这就是我设法删除并在另一个 function 中向我的模型中添加加密货币的方法。

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

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