我不确定您具体要做什么,但这是一个最小的工作示例(没有 JS 或 AJAX),您应该能够对其进行修改以满足您的需要。
它使用设置一个最大年龄的cookie的原则,例如here
使这项工作的主要内容是将 html 中的 http-equiv="refresh" 元标记设置为与 cookie 的 max_age 相同的时间段。这样,页面将在 cookie(我们设置的)过期时自动刷新,并且页面将重定向而无需进一步输入。
views.py
from django.shortcuts import redirect, render
def initial_page(request):
# Set the cookie
response = redirect("video-call")
# You probably want to set it only once for each user and make this secure
response.set_cookie(
"call_timeout",
"1",
max_age=3600 # Set this to the time you need
)
return response
def video_call_page(request):
# See if the cookie has expired
# (This will auto-check because of the http-equiv="refresh" in the HTML)
try:
c = request.COOKIES['call_timeout'] # Try to access the cookie
context = {}
# The cookie hasn't expired, so continue
return render(request, "vid_call.html", context)
except KeyError:
# The time is up, so redirect to the page of your choice
return redirect("expire_redirect")
def redirect_page(request):
# The cookie expired
context = {}
return render(request, "redirect.html", context)
urls.py
from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from .views import initial_page, video_call_page, redirect_page
urlpatterns = [
url(r'^$', initial_page, name='initial'),
url(r'^video-call/$', video_call_page, name='video-call'),
url(r'^expired/$', redirect_page, name='expire_redirect'),
path('admin/', admin.site.urls),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
vid_call.html
{% load static %}
<!doctype html>
<html lang="en-US">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<!-- Set http-equiv="refresh" to the same as the max_age param -->
<meta http-equiv="refresh" content="3600">
</head>
<body>
<h1>Video call page</h1>
<video width="320" height="240" autoplay>
<!-- Set the path to a test video in your static dir (represents your video call) -->
<source src="{% static 'mov_bbb.mp4' %}" type="video/mp4">
Your browser does not support the video tag.
</video>
</body>
</html>
redirect.html 只是您希望用户在一小时(或任何其他时间段等)后重定向到的页面内容,因此我不会在此处包含它。
谢谢。