thro avvay
thro avvay

Reputation: 15

FastAPI NameError: name 'Request' is not defined

I'm loosely following a turorial on building a full stack trading app and trying to run this script with FastAPI and uvicorn. I really can't find my mistake and also don't really know what I'm doing so any help is really apprecciated.

The code:

import sqlite3, config
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates

app = FastAPI()
templates = Jinja2Templates(directory="templates")

@app.get("/")
def index(request: Request):
    connection = sqlite3.connect(config.DB_FILE)
    connection.row_factory = sqlite3.Row
    cursor = connection.cursor()

    cursor.execute("""
        SELECT id, symbol, name FROM stock order by symbol
    """)

    rows = cursor.fetchall()

    return templates.TemplateResponse("index.html", {"request": request, "stocks": rows})

@app.get("/stock/{symbol}")
def index(request: Request, symbol):
    connection = sqlite3.connect(config.DB_FILE)
    connection.row_factory = sqlite3.Row
    cursor = connection.cursor()

    cursor.execute("""
        SELECT id, symbol, name FROM stock WHERE symbol = ?
    """, (symbol,))

    row = cursor.fetchall()

    return templates.TemplateResponse("stock_detail.html", {"request": request, "stock": row})

The error

line 9, in <module>
    def index(request: Request):
NameError: name 'Request' is not defined

Thanks so much for taking the time

Upvotes: 0

Views: 5737

Answers (2)

julianofischer
julianofischer

Reputation: 195

You need to import the Request class.

Please, change de line 2 to: from fastapi import FastAPI, Request

Upvotes: 2

astrochun
astrochun

Reputation: 1796

You need to import Request. Replaced you first line:

from fastapi import FastAPI, Request

Upvotes: 0

Related Questions