Javascript 在python中如何将数据从一个文件传递到另一个文件?
我的html代码是(ser.html) 我的URL.py是Javascript 在python中如何将数据从一个文件传递到另一个文件?,javascript,jquery,python,python-3.x,vue.js,Javascript,Jquery,Python,Python 3.x,Vue.js,我的html代码是(ser.html) 我的URL.py是 from django.conf.urls import url from . import views urlpatterns = [ url(r'^abt', views.abt), url(r'^$', views.index), url(r'^ser',views.ser), url(r'^det',views.det), ] My det.html包含以下代码 <script
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^abt', views.abt),
url(r'^$', views.index),
url(r'^ser',views.ser),
url(r'^det',views.det),
]
My det.html包含以下代码
<script>
mounted() {
var self = this;
$.ajax({
url: 'post/filter/',
data: {
category: {{name}},
},
type: "POST",
dataType: 'json',
success: function (e) {
if (e.status == 1) {
window.searchContent.vector = e.data;
console.log(e);
}
}
});
</script>
在
view.py
文件中,渲染器不知道从何处获取name
变量。这里有一种解决方法:您可以将ser.html
中的name
参数作为GET param传递到后端:
<div>
<a v-bind:href="'searchnew/?name='+post.name">
{{post.name}}</a>
</div>
在det.html
中,它将可用
如果要使用POST请求执行此操作,请将ser.html更改为运行VueJS函数,如下所示:
<div>
<a v-on:click="funcToPost">{{post.name}}</a>
</div>
var app = new Vue({
// ... some code here
methods: {
funcToPost: function() {
// ... some code here
$.ajax({
// ... some code here
data: {
name: {{name}}
}
})
}
}
})
在view.py中将request.GET.GET('name')
更改为request.POST.GET('name')
希望这能解决你的问题。祝您好运。您需要将数据从后端传递到前端。请看下面的讨论:我只是在使用APIOK,我在下面给出了一个答案(您的代码没有正确地传递变量,并且在后端也没有正确地处理它)。希望它能解决您的问题。当我使用request.POST方法时,如何更改为request-POST方法changing@coder我没有尝试解决方案,可能有一些拼写错误,但一般的解决方案必须包括你想要的。最好的!先生,我还有两份档案。。我需要将'name'从ser.html传递到det,htmlsir,我认为您已经提供了相同的名称,以便在ser.html中发送post请求
<div>
<a v-bind:href="'searchnew/?name='+post.name">
{{post.name}}</a>
</div>
def det(request):
name = request.GET.get('name')
return render(request,"det.html",{'name' :name})
<div>
<a v-on:click="funcToPost">{{post.name}}</a>
</div>
var app = new Vue({
// ... some code here
methods: {
funcToPost: function() {
// ... some code here
$.ajax({
// ... some code here
data: {
name: {{name}}
}
})
}
}
})