Javascript 如何使用django变量嵌入twitch流
我试图将twitch steam嵌入到我的网站中,但我希望流名称继承自Django模型变量 这就是我正在尝试使用的HTMLJavascript 如何使用django变量嵌入twitch流,javascript,python,html,django,twitch,Javascript,Python,Html,Django,Twitch,我试图将twitch steam嵌入到我的网站中,但我希望流名称继承自Django模型变量 这就是我正在尝试使用的HTML {% extends 'base.html' %} {% block Title %} View Booth {% endblock %} {% load static %} {% block header %} <link rel="stylesheet" href="{% static 'css/booth_profile.cs
{% extends 'base.html' %}
{% block Title %} View Booth {% endblock %}
{% load static %}
{% block header %}
<link rel="stylesheet" href="{% static 'css/booth_profile.css' %}">
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Redressed&display=swap" rel="stylesheet">
<script src="https://player.twitch.tv/js/embed/v1.js"></script>
{% endblock header %}
{% block body %}
<h1 class="fullname">{{ booth.name }}</h1>
<img src="{{ booth.cover_image.url }}" class="cover centered" alt="cover pic">
<div class="mb-4">
{% ifequal booth user.booth.first %}
<a href="{% url 'new_artwork' booth.pk %}" class="btn btn-primary">New Artwork</a>
{% endifequal %}
Live Stream:
<p id="demo">{{ booth.twitch_name }}</p>
<div id="twitch-embed"></div>
<script src="https://embed.twitch.tv/embed/v1.js"></script>
<div id="twitch-embed">
{# <iframe#}
{# id="chat_embed"#}
{# src="https://www.twitch.tv/embed/lec/chat?parent=www.google.com"#}
{# height="500"#}
{# width="350">#}
{#</iframe>#}
</div>
</div>
<div class="details">
<b>Name:</b> {{booth.name}}<br>
<b>Description:</b> {{ booth.description }}
</div>
{% for artwork in booth.artworks.all %}
<br>
Name: {{ artwork.name }}<br>
Description: {{ artwork.description }}<br>
<img src="{{ artwork.image.url }}" class="cover centered" alt="cover pic">
<br>
Price: {{ artwork.price }}<br>
{% endfor %}
<script type="text/javascript">
var twitch_name = {{ booth.twitch_name }};
new Twitch.Player("twitch-embed", {
width: 854,
height: 480,
channel: twitch_name
});
</script>
{% endblock %}
以及以下观点:
def viewBooth(request, pk):
booth = Booth.objects.get(pk=pk)
return render(request, 'viewbooth.html', {'booth': booth})
到目前为止,如果我在twitch_name javascript变量中手动写入流名称,它可以正常工作,但一旦我切换到django模型,它就会中断,我不知道原因。在使用django变量时,您是否尝试过检查同一元素的值以查看它是否存在?
def viewBooth(request, pk):
booth = Booth.objects.get(pk=pk)
return render(request, 'viewbooth.html', {'booth': booth})