我试图把一个网址基于我的views.py,但它返回错误:
找不到'transmition'的反转。'transmition'不是有效的视图函数或模式名称。
项目urls.py:
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from plataforma.views import *
from django.views.generic.base import TemplateView
urlpatterns = [
path("admin/", admin.site.urls),
path('contas/', include('django.contrib.auth.urls')),
path('', TemplateView.as_view(template_name='index.html'), name='index'),
path('plataforma/', TemplateView.as_view(template_name='plataforma.html'), name='plataforma'),
path('plataforma/stream', TemplateView.as_view(template_name='stream.html'), name='stream'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
应用程序urls.py:
from django.urls import path
from .views import *
urlpatterns = [
path('plataforma', PlataformaView.as_view(), name='plataforma'),
path('stream/', StreamView.as_view(), name='stream'),
path('transmition/', transmition, name='transmition'),
]
应用程序views.py:
from django.shortcuts import render, redirect
from django.contrib import messages
from django.core.mail import EmailMessage
from django.views.decorators import gzip
from django.views.generic import TemplateView
from django.http import StreamingHttpResponse
import cv2
import threading
@gzip.gzip_page
def transmition(request):
try:
cam = VideoCamera()
return StreamingHttpResponse(gen(cam), mimetype="multipart/x-mixed-replace;boundary=frame")
except:
pass
# return render(request, 'stream.html')
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(
"rtsp://jorge:jorge33621005@189.124.26.190:554/cam/realmonitor?channel=1&subtype=1")
(self.grabbed, self.frame) = self.video.read()
threading.Thread(target=self.update, args=()).start()
def __del__(self):
self.video.release()
def get_frame(self):
image = self.frame
_, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()
def update(self):
while True:
(self.grabbed, self.frame) = self.video.read()
def gen(camera):
while True:
frame = camera.get_frame()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
class PlataformaView(TemplateView):
template_name = 'plataforma.html'
class StreamView(TemplateView):
template_name = 'stream.html'
数据流.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Test</h1>
<img src="{% url 'transmition' %}">
</body>
</html>
由于我是一个初学者,我不能在HTML页面中传输RTSP流。我需要流和样式的'传输'功能。
1条答案
按热度按时间hc8w905p1#
您可以在应用的www.example.com中定义
app_name
urls.py,如下所示:然后在URL标记中调用时使用它,如下所示:
您可以根据需要给予任何
app_name
。编辑
您还应该删除项目的www.example.com中不必要的路由urls.py,因此它应该如下所示: