generated from hjess/PythonTemplateProject
Loads of boiler plating
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
Some checks failed
Build, Push, and Deploy to Nomad / docker-nomad (push) Has been cancelled
This commit is contained in:
30
app.py
30
app.py
@@ -20,17 +20,29 @@ with open("mock_data.json") as file:
|
||||
# Index route
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def get_index(request: Request):
|
||||
return templates.TemplateResponse("index.html", {"request": request, "data": data})
|
||||
return templates.TemplateResponse(
|
||||
"index.html",
|
||||
{"request": request, "data": data, "page_title": "Forside", "author": "Henrik"}
|
||||
)
|
||||
|
||||
# Category route
|
||||
@app.get("/category/{category_name}", response_class=HTMLResponse)
|
||||
async def get_category(request: Request, category_name: str):
|
||||
category_file = f"data/{category_name}/index.html"
|
||||
if os.path.exists(category_file):
|
||||
with open(category_file) as file:
|
||||
category_content = file.read()
|
||||
return templates.TemplateResponse(
|
||||
"category.html",
|
||||
{"request": request, "category_name": category_name, "content": category_content}
|
||||
)
|
||||
# Find den korrekte kategori
|
||||
category = next((cat for cat in data["categories"] if cat["path"] == category_name), None)
|
||||
if category:
|
||||
category_file = f"data/{category_name}/index.html"
|
||||
if os.path.exists(category_file):
|
||||
with open(category_file) as file:
|
||||
category_content = file.read()
|
||||
return templates.TemplateResponse(
|
||||
"category.html",
|
||||
{
|
||||
"request": request,
|
||||
"data": data,
|
||||
"page_title": category["name"],
|
||||
"author": category["author"],
|
||||
"content": category_content
|
||||
},
|
||||
)
|
||||
return HTMLResponse("Kategori ikke fundet", status_code=404)
|
||||
Reference in New Issue
Block a user