html jinja2.exceptions.templatesyntaxerror:预期令牌':' got'}'错误

发布于 2025-01-22 14:05:48 字数 703 浏览 2 评论 0原文

我使用Pycharm通过烧瓶创建了本地服务器。但是我想创建一个动态链接,此链接将是指向其他网站的链接。当我想提供直接链接时,它会尝试在我自己的服务器上连接。但是我不想要这个,我想访问Spotify,YouTube等网站。经过一番研究,我找到了类似于下面代码的代码。但是我收到了一条错误消息。

错误消息

jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

我的html代码在这里

<dd>
    <a href = {{ url_for('go_outside_flask_variable', variable={{music_link}} ) }}>Link</a>
</dd>

我的python代码在这里

@app.route('/go_outside_flask/<variable>')
def go_outside_flask_method(variable):
    redirect(variable, code=302)

注意:音乐链接就像“ open.spotify/scotify/tracks/****” *”

I created a local server via Flask using Pycharm. But I want to create a dynamic link and this link will be a link to other websites. When I want to give a direct link, it tries to connect on my own server. But I don't want this, I want to access websites such as spotify, youtube. After some research, I found a code similar to the code below. But I am getting an error message.

ERROR MESSAGE

jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

MY HTML CODE IS HERE

<dd>
    <a href = {{ url_for('go_outside_flask_variable', variable={{music_link}} ) }}>Link</a>
</dd>

MY PYTHON CODE IS HERE

@app.route('/go_outside_flask/<variable>')
def go_outside_flask_method(variable):
    redirect(variable, code=302)

Note: Music link is like "open.spotify/tracks/*****"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空名 2025-01-29 14:05:48

它是由于变量= {{music_link}}引起的,

您可以尝试执行此操作
html

  <a href = {{ url_for('go_outside_flask_method', variable="{}".format(music_link) ) }}>Link</a>  

python

@app.route("/test")
def testing():
  return render_template("test.html", music_link = 134955)

@app.route('/go_outside_flask/<variable>')
def go_outside_flask_method(variable):
    return redirect(variable, code=302)

输出

Link that have an address of (/go_outside_flask/134955)

---编辑以适合需求---

open.spotify.com/track/1QZRCi2Z1DQQaR6bGAzhtN

,如果您只想重定向到此链接,请执行

html

<a href = "{{music_link}}">Link</a>

python

@app.route("/help")
def helping():
  return render_template("testerer.html", music_link = "https://open.spotify.com/track/2q3LzfknBzye3xXjgiJBjS?si=pzB7JVqDS0-5Gel18Jm9VQ&utm_source=copy-link&context=spotify%3Aplaylist%3A37i9dQZF1DXcZQSjptOQtk")

It is caused due to the variable={{music_link}}

You could try doing this
Html

  <a href = {{ url_for('go_outside_flask_method', variable="{}".format(music_link) ) }}>Link</a>  

Python

@app.route("/test")
def testing():
  return render_template("test.html", music_link = 134955)

@app.route('/go_outside_flask/<variable>')
def go_outside_flask_method(variable):
    return redirect(variable, code=302)

Output

Link that have an address of (/go_outside_flask/134955)

---Edit to fit the need---

open.spotify.com/track/1QZRCi2Z1DQQaR6bGAzhtN

Now if you want to only redirect to this link just do

HTML

<a href = "{{music_link}}">Link</a>

Python

@app.route("/help")
def helping():
  return render_template("testerer.html", music_link = "https://open.spotify.com/track/2q3LzfknBzye3xXjgiJBjS?si=pzB7JVqDS0-5Gel18Jm9VQ&utm_source=copy-link&context=spotify%3Aplaylist%3A37i9dQZF1DXcZQSjptOQtk")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文