gpt4 book ai didi

javascript - Django:如何将 javascript var 作为 ajax 中的参数传递给 Django URL?

转载 作者:行者123 更新时间:2023-11-30 20:57:38 30 4
gpt4 key购买 nike

如何将 javascript 变量传递给 Django URL?它呈现了一个,

TypeError at /object/Query/details

view() got an unexpected keyword argument 'query'

我正尝试按照此过程重定向到另一个 HTML 页面:

HTML 搜索输入 > 通过输入按钮传递给 Javascript onclick > 传递给 JQuery Ajax sendQueryData > 使用 Django 重定向到另一个页面 render();考虑到我必须在 AJAX 中传递给 URL 的参数上做更多的事情。

主要问题在于这两行(我认为):

url_page = url + "{% url 'view' 'query'%}"; //supposedly how we get parameters (but query is a javascript `var`

url =url.replace('query' , query); //where I intend to correctly build the url forcing the query to take its place in the url

我只是不太确定我的 urls.py ,在 Django url 中对 javascript 的 ajax 调用完全对齐。

我的 urls.py

urlpatterns = [
url(r'^$', views.index, name='homepage'),

#--- Home ---
url(r'^search/pass/$', views.pass_query, name='pass_query'),

# View Object Details
url(r'^object/(?P<query>\w+)/details', views.view(), name='view')

我的 HTML

<div id="button-group" class="button-group">
<input type="submit" class="btn btn-primary btn-lg btn-space col-sm-3" onclick="redirect_to_url(0)" value="View Inventory"/>
<input type="submit" class="btn btn-primary btn-lg btn-space col-sm-3" onclick="redirect_to_url(1)" value= "View Details"/>

Javascript

function redirect_to_url(to_page){
var query = document.getElementById("search").value;
var url = document.URL;
url = url.slice(0, -1);

if (to_page === 0) {
url_page = url + "{% url 'view' 'query'%}";
sendQueryData(url_page, query);
}
else if(to_page === 1) {
url_page = url + "{% url 'another_view' 'query' %}";
sendQueryData(url_page, query);

}
return false;
}

function sendQueryData(url, query){
url =url.replace('query' , query);

if (query === ""){
alert("Input Required");
}else{

$.ajax({
beforeSend: function(xhr, settings) {
if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", csrftoken);
}
},
type: "POST",
url: "/search/pass/",
data: {
'query' : query,
'url' : url
},
success: function(result){
alert(result);
window.location.href = url;
}
});
}

}

View .py

def pass_query(request):
query =request.POST['query']
print(query)
return HttpResponse(query)

def view(request):
return render(request, 'plexus/view.html')

Edit:

The error occurs at AJAX Success function. If I debugged it correctly, I tried to change the url in window.location.href = to another static URL that doesn't have a parameter unlike url(r'^object/(?P<query>\w+)/details', views.view, name='view'), and it does work well. If this is so, maybe I can re-title this on how to add a parameter to the url where Django URL is looking for an argument.

最佳答案

您需要在views.view 定义中添加一个query 参数:

def view(request, query):

如果可以在不提供查询的情况下调用view,那么您可以将其定义为:

def view(request, query=None):

关于javascript - Django:如何将 javascript var 作为 ajax 中的参数传递给 Django URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47491759/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com