我正在开发一个允许移动相机的网页。我创建了一些按钮,所以无论何时按下它,我们都会转到urls.py“moveRight”然后转到views.py.在views.py move()函数(这是一个python脚本)中移动一个摄像头,然后我回到按钮所在的第一页。
panel.py
<form action="{% url 'cameracontrol:moveRight' %}" method="post">
{% csrf_token %}
<button type="submit" id="right" class="btn btn-default" aria-label="Move right"></button>
</form>
urls.py
urlpatterns = [
url(r'^$', views.panelView, name='panel'),
url(r'^right/$', views.moveCameraRight, name='moveRight'),
]
views.py
def moveCameraRight(request): #
move()
return HttpResponseRedirect('/cameracontrol/')
但我需要运行/ cameracontrol / page而不重新加载。我看到了关于ajax的一些内容,但无法弄清楚如何在这里使用它
答案 0 :(得分:0)
此解决方案应该有效。
$('#action-button').click(function() {
$.ajax({
url: 'http://api.joind.in/v2.1/talks/10889',
success: function(data) {},
type: 'GET'
});
});
def moveCameraRight(request): #
move()
return JsonResponse({'move': 'right'})
答案 1 :(得分:0)
在你的html文件中,你可以添加一些JavaScript来进行Ajax调用。我正在添加JQuery库,因为它可以更轻松地使用JavaScript
internal delegate void CreateOfferDelegate();
internal event CreateOfferDelegate CreateOffer;
public UserControl1()
{
InitializeComponent();
}
private void btnCreateOffer_Click(object sender, EventArgs e)
{
CreateOffer();
}
这只是一个基本的例子。 JQuery代码是什么,“点击ID为'right'的元素时,调用给定的函数。”而这个功能轮流调用你的网址 在这个例子中,我使用了GET而不是post,因为你的代码没有显示任何需要使用post(除了可能只有某些用户可以移动相机)。为了使它发布,您只需将对象传递给ajax调用
<!--somefile.html-->
<html>
<head>....</head>
<body>
<button id="right" class="btn btn-default" aria-label="Move right"></button>
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
$(document).ready(function(){
$('#right').on('click', function() {
$.ajax({% url 'cameracontrol:moveRight' %});
});
});
</script>
</body>
</html>
但您还需要在标头中包含CSRF令牌。我通常会做以下
$.ajax({
url: url,
method: 'POST',
});