簡體   English   中英

嘗試運行 python 代碼時出現語法錯誤(數組)

[英]SyntaxError while trying to run python code (Array)

我收到一個錯誤:

File "/home/ofw/playlister/app.py", line 7
    playlists = [
    ^
SyntaxError: invalid syntax

這是我的 app.py 代碼:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
playlists = [
    { 'title': 'Cat Videos', 'description': 'Cats acting weird' },
    { 'title': '80\'s Music', 'description': 'Don\'t stop believing!' }
]

@app.route('/)
def playlists_index():
    """Show all playlists."""
    return render_template('playlists_index.html', playlists=playlists)

我猜它與播放列表數組有關。 有什么建議么? 提前,謝謝。

您在第 5 行使用裝飾器,但下面沒有要裝飾的內容。 必須有一個函數/方法。 第 11 行的那個是正確的。

您的播放列表變量的定位是錯誤的,因為它直接在裝飾器“@app.route”之下。裝飾器之后應該是 function。 嘗試以這種方式編輯代碼:

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/)
def playlists_index():
    """Show all playlists."""
    playlists = [
    { 'title': 'Cat Videos', 'description': 'Cats acting weird' },
    { 'title': '80\'s Music', 'description': 'Don\'t stop believing!' }
    ]

    return render_template('playlists_index.html', playlists=playlists)

或以這種方式全局創建播放列表變量:

playlists = [
    { 'title': 'Cat Videos', 'description': 'Cats acting weird' },
    { 'title': '80\'s Music', 'description': 'Don\'t stop believing!' }
    ]
@app.route('/')
def playlists_index():
    """Show all playlists."""
    return render_template('playlists_index.html', playlists=playlists)

不要忘記將第 11 行的路由字符串更正為:

@app.route('/')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM