Er gaat iets mis
Klik op je probleem om de oplossing te zien.
Server starten
fastapi: command not found
FastAPI is niet geinstalleerd. Run:
pip install "fastapi[standard]"
Zorg dat je in je virtual environment zit (je ziet (.venv) in de terminal).
ModuleNotFoundError: No module named 'fastapi'
Je zit niet in je virtual environment. Check of je (.venv) ziet in de terminal. Zo niet, activeer het:
# Windows
.venv\Scripts\activate
# Mac/Linux
source .venv/bin/activate
Address already in use (poort 8000 bezet)
Er draait al een server op poort 8000. Sluit de andere terminal of gebruik een andere poort:
fastapi dev main.py --port 8001
Server start maar pagina laadt niet
- Check of je naar
http://127.0.0.1:8000gaat (niethttps) - Check of de server nog draait in de terminal
- Probeer de pagina te refreshen (Ctrl+F5)
HTML & CSS
CSS werkt niet / styling is weg
Check:
- Heb je
app.mount("/static", StaticFiles(directory="static"), name="static")in je code? - Staat je CSS bestand in
static/css/style.css? - Staat in je HTML:
<link rel="stylesheet" href="/static/css/style.css">? - Herstart de server en doe een hard refresh (Ctrl+F5)
404 Not Found bij een pagina
- Check of het pad in
FileResponse("...")klopt - Check of het bestand echt op die plek staat
- Let op hoofdletters in bestandsnamen!
Afbeelding laadt niet (broken image)
- Staat de afbeelding in de
staticfolder? - Klopt de bestandsnaam exact? (hoofdletters tellen!)
- Klopt het pad in
src="/static/foto.jpg"? - Heb je
app.mount("/static", ...)in je code?
HTML wordt als tekst getoond (je ziet de tags)
Je mist response_class=HTMLResponse:
# Fout - toont HTML als tekst
@app.get("/pagina")
async def pagina():
return "<h1>Hallo</h1>"
# Goed - toont HTML als pagina
@app.get("/pagina", response_class=HTMLResponse)
async def pagina():
return "<h1>Hallo</h1>"
Formulieren & POST
422 Unprocessable Entity
Dit betekent dat FastAPI de form data niet kan verwerken. Check:
- Heb je
from fastapi import Formgeimporteerd? - Staat
name="..."op je<input>tags? - Matcht de
namein HTML met de parameter in Python? - Staat
method="post"op je<form>tag?
Form data komt niet aan
Check:
method="post"in de form tag?action="/juiste_endpoint"in de form tag?name="veldnaam"op elk input veld?- Parameter naam in Python matcht met
namein HTML?
Voorbeeld:
<input name="naam"> <!-- HTML -->
naam: str = Form(...) # Python - moet "naam" zijn!
Method Not Allowed (405)
Je stuurt een POST naar een GET endpoint of andersom. Check:
- Formulier heeft
method="post"→ endpoint moet@app.post(...)zijn - Browser URL bezoeken is altijd GET → endpoint moet
@app.get(...)zijn
Templates (Jinja2)
TemplateNotFound error
- Staat je template in de
templatesfolder? - Klopt de bestandsnaam in
TemplateResponse("bestand.html", ...)? - Heb je
templates = Jinja2Templates(directory="templates")in je code?
Template variabele toont niets
Check of je de variabele meestuurt in het dictionary:
# Fout - naam niet meegestuurd
return templates.TemplateResponse("pagina.html", {"request": request})
# Goed - naam meegestuurd
return templates.TemplateResponse("pagina.html", {"request": request, "naam": naam})
TypeError: context must include a "request" key
Je bent "request": request vergeten:
# Fout
return templates.TemplateResponse("pagina.html", {"naam": naam})
# Goed
return templates.TemplateResponse("pagina.html", {"request": request, "naam": naam})
Database (sqlitedict)
ModuleNotFoundError: No module named 'sqlitedict'
Installeer het:
pip install sqlitedict
Check dat je in je virtual environment zit!
Data is weg na herstarten
Je bent db.commit() vergeten:
with SqliteDict("data.db") as db:
db["key"] = "waarde"
db.commit() # DIT NIET VERGETEN!
KeyError bij uitlezen
De key bestaat niet. Gebruik db.get() met een default waarde:
# Fout - crasht als key niet bestaat
waarde = db["naam"]
# Goed - geeft "Onbekend" als key niet bestaat
waarde = db.get("naam", "Onbekend")
Algemeen
Wijzigingen zijn niet zichtbaar
- Herstart de server (Ctrl+C, dan opnieuw
fastapi dev main.py) - Hard refresh in browser (Ctrl+F5)
- Check of je het juiste bestand hebt aangepast
ImportError / NameError
Je bent een import vergeten. Meest voorkomende imports:
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from sqlitedict import SqliteDict
IndentationError
Python is streng op inspringen. Gebruik overal dezelfde hoeveelheid spaties (4 spaties is standaard). Mix niet tabs en spaties.