Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据用户输入自动嵌入多个不同的youtube视频?(谷歌应用引擎)_Python_Html_Youtube Api_Google Cloud Datastore - Fatal编程技术网

Python 如何根据用户输入自动嵌入多个不同的youtube视频?(谷歌应用引擎)

Python 如何根据用户输入自动嵌入多个不同的youtube视频?(谷歌应用引擎),python,html,youtube-api,google-cloud-datastore,Python,Html,Youtube Api,Google Cloud Datastore,我正在尝试使用youtube的iframe代码自动嵌入youtube视频,该代码被分类为不同的类型。用户可以输入一个将被存储的youtube共享url,在将其解析为html中的iframe代码之前,它应该获取符合类型的四个最新条目并截断url。以下是我到目前为止的情况: 用于存储输入: class suggestionndb(webapp2.RequestHandler): def post(self): suggestion = suggestions()

我正在尝试使用youtube的iframe代码自动嵌入youtube视频,该代码被分类为不同的类型。用户可以输入一个将被存储的youtube共享url,在将其解析为html中的iframe代码之前,它应该获取符合类型的四个最新条目并截断url。以下是我到目前为止的情况:

用于存储输入:

class suggestionndb(webapp2.RequestHandler):
    def post(self):
        suggestion = suggestions()
        suggestion.link = self.request.get("suggestlink")
        suggestion.genre = self.request.get("suggestgenre")
        suggestion.date = datetime.datetime.now()
        suggested = suggestion.put()`
要截断和解析要自动嵌入的url,请执行以下操作:

class classicalpage(webapp2.RequestHandler):
def get(self):
    user = users.get_current_user()
    if user:
        logout = users.create_logout_url(self.request.uri)
        classical = suggestions.query(suggestions.genre == "classical").order(-suggestions.date)
        classicallist = classical.fetch(4)
        for i in classicallist:
            link = i.link
            embed = link[16:]
            self.response.write(embed)
            template_values = {"logouturl":logout, "link":embed}
            template = JINJA_ENVIRONMENT.get_template("classical.html")
            self.response.write(template.render(template_values))
    else:
        self.redirect("/")
以下是HTML:

<div id="genrelist" name="genrelist">
        <h2 id="genretitle">♪ Classical ♪</h2>
        <iframe id="video1" name="classical1" width="480" height="360" src="https://www.youtube.com/embed/{{link}}" frameborder="0" allowfullscreen></iframe>
        <iframe id="video2" name="classical2" width="480" height="360" src="https://www.youtube.com/embed/{{link}}" frameborder="0" allowfullscreen></iframe>
        <iframe id="video3" name="classical3" width="480" height="360" src="https://www.youtube.com/embed/{{link}}" frameborder="0" allowfullscreen></iframe>
        <iframe id="video4" name="classical4" width="480" height="360" src="https://www.youtube.com/embed/{{link}}" frameborder="0" allowfullscreen></iframe>
</div>

♪ 古典的♪

这样,所有4个视频都是同一个youtube视频。。。如何根据最近的4个用户输入将每个视频制作成不同的视频?

问题在于,您在模板中使用了相同的变量(
{{link}
),而该变量只有您分配给它的一个值。您可以用几种方法解决这个问题,但使用数组可能是最好的

您需要将上下文构建代码更改为:

class classicalpage(webapp2.RequestHandler):
def get(self):
    user = users.get_current_user()
    if user:
        logout = users.create_logout_url(self.request.uri)
        classical = suggestions.query(suggestions.genre == "classical").order(-suggestions.date)
        classicallist = classical.fetch(4)
        template_values = {
            "logouturl": logout, 
            "links": [i.link[16:] for i in classicallist],
        }

        template = JINJA_ENVIRONMENT.get_template("classical.html")
        self.response.write(template.render(template_values))
    else:
        self.redirect("/")
类似地,可以使用渲染器渲染iFrame:

<div id="genrelist" name="genrelist">
    <h2 id="genretitle">♪ Classical ♪</h2>
    {% for link in links %}
        <iframe id="video{{loop.index}}" name="classical{{loop.index}}" width="480" height="360" src="https://www.youtube.com/embed/{{link}}" frameborder="0" allowfullscreen></iframe>
    {% endfor %}
</div>

♪ 古典的♪
{%用于链接中的链接%}
{%endfor%}