Tôi có một vòm máy ảnh vòm intelbras VIP 5220 SD IR mà tôi có thể di chuyển vị trí của nó [chức năng PTZ] bằng mã Python. Tuy nhiên, tôi muốn di chuyển vị trí của nó bằng một nút trên trang web. Tôi tạo một trang web bằng Django, vì vậy tôi muốn nhấp vào nút và tôi muốn máy ảnh của mình di chuyển sau đó, nhưng tôi không biết cách gọi chức năng Python của mình bằng Django. Tôi tìm thấy một số từ khóa như kênh Django, Ajax và Websockets, nhưng mọi thứ trông rất phức tạp.
Để làm điều này, tôi đã làm điều này cho đến nay:
HTML:
$[document].ready[function[]{
$["button"].click[function[]{
//alert["Botao foi clicado"];
$.ajax[{
type: "POST",
url: "/ajax_move_camera/",
data:{
"var1":"val1"
},
dataType: 'json',
sucess: function[data]{
alert["Sucess"];
},
error: function[data]{
alert["Error"]
}
}]
}];
}];
VIEWS.py
def ajax_move_camera[request]:
x=10
data={}
return JsonResponse[data]
URL.py
urlpatterns = [
url[r'^$', views.index, name='index'],
url[r'^$', views.ajax_move_camera, name='ajax_move_camera']
]
DjangoCách chạy tập lệnh Python bằng cách nhấp vào nút HTML
yêu cầu
Bước 1: Cài đặt PIP Django
Bước 2: Mở cửa sổ nhắc nhở của bạn và tạo một thư mục gọi là "Django-Webapp"
Bước 3: Trong cửa sổ nhắc nhở của bạn, điều hướng đến Django-Webapp & Run Django-Admin StartProject GeniusVoice [hoặc tên dự án Django của riêng bạn]
cd django-webapp
django-admin startproject geniusvoice
Bước 4: Điều hướng đến GeniusVoice
Điểm kiểm tra: Bước 1-4
Bước 5: Tạo một thư mục gọi là "my_templates"
Bước 6: Tạo một tệp được gọi là "GeniusVoice.html" Trong thư mục "My_Templates"
cd. > my_templates/geniusvoice.html
Bước 7: Tạo một tệp có tên là "WebApp.Py" trong thư mục "GeniusVoice"
cd. > geniusvoice/geniusvoice.html
Điểm kiểm tra: Bước 5-7
Bước 8: Sao chép và dán mã này vào urls.py.
Lưu ý: Đảm bảo thay thế mã hiện có trong urls.py
"""geniusvoice URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
//docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path['', views.home, name='home']
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path['', Home.as_view[], name='home']
Including another URLconf
1. Import the include[] function: from django.urls import include, path
2. Add a URL to urlpatterns: path['blog/', include['blog.urls']]
"""
from django.contrib import admin
from django.conf.urls import url
from . import webapp
urlpatterns = [
url[r'^admin/', admin.site.urls],
url[r'^$', webapp.button],
url[r'^output', webapp.output,name="script"],
]
Bước 8: Sao chép và dán mã này vào webapp.py.
from django.shortcuts import render
def button[request]:
return render[request,'geniusvoice.html']
def output[request]:
output_data = "Genius Voice eliminates friction. For years people have had to learn to interact with computers, we turn this around. We teach computers how to interact with humans through voice. This creates a seamless experience without losing the human touch."
website_link = "Visit our website: " + "//www.geniusvoice.nl/"
return render[request,"geniusvoice.html",{"output_data":output_data, "website_link":website_link}]
Bước 9: Sao chép và dán mã này vào GeniusVoice.html.
Genius Voice script
Click To Execute Script
{{output_data}}
{{website_link}}
Bước 10: Mở cài đặt.py, cuộn xuống "Mẫu" và dán 'my_templates' trong danh sách trống
Bước 11: Mở cửa sổ nhắc của bạn và chạy mã này:
Bước 12: Chạy mã này trong cửa sổ nhắc nhở của bạn:
python manage.py runserver
Copy and paste //127.0.0.1:8000/ in your browser