I'm trying to put a url based on my views.py but it returns the error:
Reverse for 'transmition' not found. 'transmition' is not a valid view function or pattern name.
project 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)
app 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'),
]
app 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:[email protected]: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'
stream.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>
As I'm a beginner, I couldn't transmit the rtsp stream in the HTML page. I need to stream and style the 'transmition' function.
You can define app_name
in app's urls.py, like so:
from django.urls import path
from .views import *
app_name='plataforma'
urlpatterns = [
path('', PlataformaView.as_view(), name='plataforma_view'),
path('stream/', StreamView.as_view(), name='stream'),
path('transmition/', transmition, name='transmition'),
]
Then use it while calling in URL tag as:
<img src="{% url 'plataforma:transmition' %}">
You can give any app_name
according to your need.
You should also remove unnecessary routes in project's urls.py, so it should like this:
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/', include('plataforma.urls'), namespace='plataforma')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)