JS前端向Django后台提交数据

添加{% csrf_token %}

HTML中添加表单并隐藏

<form id="myForm" action="detail" method="post" style="display:none">
{% csrf_token %}
<input id="myText" type="text" name="q" value="Mickey">
</form>

JS中填写数据并提交

let location = event.data.物料编码;
document.getElementById("myText").value = location;
document.getElementById("myForm").submit();

django后台接收数据

def detail(request):  
    ctx ={}
    if request.POST:
        ctx['rlt'] = request.POST['q']
    return  render(request,"detail.html", ctx)

前端显示接收数据

<p>{
   
   { rlt }}</p>

猜你喜欢

转载自blog.csdn.net/GQ_Sonofgod/article/details/122292189