Wie erstelle ich einen Rückruf, wenn die 404-Seite ausgelöst wird?Python

Python-Programme
Anonymous
 Wie erstelle ich einen Rückruf, wenn die 404-Seite ausgelöst wird?

Post by Anonymous »

Ich versuche, einen Rückruf zu erstellen, um die URL zu senden, die der Benutzer verwendet hat, als er die 404-Seite erreicht hat. Bei anderen Seiten höre ich normalerweise auf die URL selbst und löse etwas aus, wenn sie mit dem Pfadnamenmuster übereinstimmt, aber im speziellen Fall der 404-Seite funktioniert das nicht, da die Seite auf nichts Bestimmtes verweist.
Sie können die folgende Seite ausprobieren:

Code: Select all

"""Code to generate the layout of a 404 error page."""

import logging

import dash
from dash import Input, Output, State, callback, html

from .. import telemetry

def layout() -> html.Div:
"""Return the layout of a 404 error page."""

# prepare the text in advance to avoid long lines
oops = "Oops! The page you are looking for does not exist."
btn = "Go Back to Main"

# create the different elements of the page
title = html.H1("404")
subtitle = html.H2("Page Not Found")
message = html.P(oops)
button = html.A(btn, href="/")

# add a telemetry tracker
tracker = html.Div(id="not_found_404_tracker", style={"display": "none"})

# create the layout of the 404 page
return html.Div([title, subtitle, message, button])

@callback(
Output("not_found_404_tracker", "children"),
# I guess it's not the right Input and should be a state instead
# and then the real question begin: how to catch when the 404 is reached
Input("url", "pathname"),
State("magic_session", "data"),
prevent_initial_call=False,
)
def track_not_found_404_visit(url, session_data):
"""Track 404 page visit with session ID."""
if session_data and session_data.get("session_id"):
telemetry.add_page_visit(
session_data["session_id"],
"not_found_404_page",
"url": url
)
return ""

dash.register_page(__name__, layout=layout)

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post