我正在使用Django开发应用程序的仪表板。在信息中心上,有指向日期的超链接,这意味着当我单击特定的日期时,我将获得该用户和该日期存在的数据。
在index.html 中:
href="{% url 'mailpieces:get_day_mailpiece' question_id=user_id day=date %}">{{ day }}
所以get_day_mailpiece函数接受两个参数并返回数据,但是我在重定向页面上获取该数据,我希望当天在同一页面上的该数据在仪表板上。
答案 0 :(得分:1)
如果您是Java新手,请尝试使用jQuery:
{% block javascript %}
<script>
$("#test").submit(function(event){
event.preventDefault();
$.ajax({
type: "GET",
url: "{% url 'mailpieces:get_day_mailpiece' question_id=user_id day=date %}",
success: function(data){
$('#result').html("<h2>" + data.responseText+ "</h2>")
}
});
return false;
});
</script>
{% endblock %}
{% block content %}
<input type="text" value={{ day }}/>
<button id="test" type="submit">Click</button>
<div id="result">Result will be displayed here</div>
{% endblock %}
答案 1 :(得分:0)
You need to use AJAX to send the request. Response will be returned as JSON which you need to parse and display relevant information.
Something like below.
$.ajax({
url : $(element).attr("data-url"),
data : {
"csrfmiddlewaretoken" : $(element).siblings("input[name='csrfmiddlewaretoken']" ).val(),
"user":user_id,
"day": day
},
method: "POST",
dataType : "json",
success : function (returned_data) {
// display data
}
});
If request is not post, no need to worry about csrf token.
In case of get request, use below code.
$.ajax({
url : $(element).attr("data-url"),
method: "GET",
dataType : "json",
success : function (returned_data) {
// display data
}});
You can trigger this ajax call on click of a button (or link). Define an attribute data-url
in button or link with value equal to {% url 'mailpieces:get_day_mailpiece' question_id=user_id day=date %}
<button data-url={% url here %} onclick=get_data_by_ajax(this); class="btn btn-link">Get data for day 1</button>
For detailed step by step process refer this article.
Just in case if you mean to say data is being displayed in new tab, remove the target=_blank
from your link.