I have a question on how to call a view function from a template HTML button? Like an onclick function? Here is the template:
我有一个关于如何从模板HTML按钮调用视图函数的问题?像onclick函数一样?这是模板:
<input id="submit" type="button" onclick="xxx" method="post" value="Click" />
And the views.py is:
而views.py是:
def request_page(request):
...do something...
return render_to_response("/directory.html", {})
Thank you very much.
非常感谢你。
5 个解决方案
#1
13
One option is, you can wrap the submit
button with a form
一个选项是,您可以使用表单包装提交按钮
Something like this:
像这样的东西:
<form action="{% url path.to.request_page %}" method="POST">
<input id="submit" type="button" value="Click" />
</form>
(remove the onclick
and method
)
(删除onclick和方法)
If you want to load a specific part of the page, without page reload - you can do
如果您想加载页面的特定部分,而无需重新加载页面 - 您可以这样做
<input id="submit" type="button" value="Click" data_url/>
and on a submit
listener
并在提交监听器上
$(function(){
$('form').on('submit', function(e){
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
method: $(this).attr('method'),
success: function(data){ $('#target').html(data) }
});
});
});
#2
66
Assuming that you want to get a value from the user input in html textbox whenever the user clicks 'Click' button, and then call a python function (mypythonfunction) that you wrote inside mypythoncode.py. Note that "btn" class is defined in a css file.
假设您想要在用户单击“Click”按钮时从html文本框中的用户输入获取值,然后调用您在mypythoncode.py中编写的python函数(mypythonfunction)。请注意,“btn”类是在css文件中定义的。
inside templateHTML.html:
在templateHTML.html中:
<form action="#" method="get">
<input type="text" value="8" name="mytextbox" size="1"/>
<input type="submit" class="btn" value="Click" name="mybtn">
</form>
inside view.py:
在view.py内:
import mypythoncode
def request_page(request):
if(request.GET.get('mybtn')):
mypythoncode.mypythonfunction( int(request.GET.get('mytextbox')) )
return render(request,'myApp/templateHTML.html')
#3
6
How about this:
这个怎么样:
<a class="btn btn-primary" href="{% url 'url-name'%}">Button-Text</a>
The class is including bootstrap styles for primary button.
该类包括主按钮的引导样式。
#4
4
you can put the input inside a form like this:-
你可以把输入放在这样的表格中: -
<script>
$(document).ready(function(){
$(document).on('click','#send', function(){
$('#hid').val(data)
document.forms["myForm"].submit();
})
})
</script>
<form id="myForm" action="/request_page url/" method="post">
<input type="hidden" id="hid" name="hid"/>
</form>
<div id="send">Send Data</div>
#5
1
For eg, a logout button can be written like this:
例如,注销按钮可以这样写:
<button class="btn btn-primary" onclick="location.href={% url 'logout'%}">Logout</button>
Where logout endpoint:
注销终点:
#urls.py:
url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),
#1
13
One option is, you can wrap the submit
button with a form
一个选项是,您可以使用表单包装提交按钮
Something like this:
像这样的东西:
<form action="{% url path.to.request_page %}" method="POST">
<input id="submit" type="button" value="Click" />
</form>
(remove the onclick
and method
)
(删除onclick和方法)
If you want to load a specific part of the page, without page reload - you can do
如果您想加载页面的特定部分,而无需重新加载页面 - 您可以这样做
<input id="submit" type="button" value="Click" data_url/>
and on a submit
listener
并在提交监听器上
$(function(){
$('form').on('submit', function(e){
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
method: $(this).attr('method'),
success: function(data){ $('#target').html(data) }
});
});
});
#2
66
Assuming that you want to get a value from the user input in html textbox whenever the user clicks 'Click' button, and then call a python function (mypythonfunction) that you wrote inside mypythoncode.py. Note that "btn" class is defined in a css file.
假设您想要在用户单击“Click”按钮时从html文本框中的用户输入获取值,然后调用您在mypythoncode.py中编写的python函数(mypythonfunction)。请注意,“btn”类是在css文件中定义的。
inside templateHTML.html:
在templateHTML.html中:
<form action="#" method="get">
<input type="text" value="8" name="mytextbox" size="1"/>
<input type="submit" class="btn" value="Click" name="mybtn">
</form>
inside view.py:
在view.py内:
import mypythoncode
def request_page(request):
if(request.GET.get('mybtn')):
mypythoncode.mypythonfunction( int(request.GET.get('mytextbox')) )
return render(request,'myApp/templateHTML.html')
#3
6
How about this:
这个怎么样:
<a class="btn btn-primary" href="{% url 'url-name'%}">Button-Text</a>
The class is including bootstrap styles for primary button.
该类包括主按钮的引导样式。
#4
4
you can put the input inside a form like this:-
你可以把输入放在这样的表格中: -
<script>
$(document).ready(function(){
$(document).on('click','#send', function(){
$('#hid').val(data)
document.forms["myForm"].submit();
})
})
</script>
<form id="myForm" action="/request_page url/" method="post">
<input type="hidden" id="hid" name="hid"/>
</form>
<div id="send">Send Data</div>
#5
1
For eg, a logout button can be written like this:
例如,注销按钮可以这样写:
<button class="btn btn-primary" onclick="location.href={% url 'logout'%}">Logout</button>
Where logout endpoint:
注销终点:
#urls.py:
url(r'^logout/$', auth_views.logout, {'next_page': '/'}, name='logout'),