How do I create a 404 page?

2024/10/15 3:10:09

My application catches all url requests with an @app.route, but occasionally I bump into a bad url for which I have no matching jinja file (bu it does match an existing @app.route). So I want to redirect such requests to a 404 page for that bad url.

How to discriminate between "a jinja file exists" and "a jinja file doesn't exist" before returning render_template()?

Answer

Jinja will throw an exception if the template is not found: TemplateNotFound

So instead of:

def myview():return render_template(...)

you could do something like this:

def myview():try:return render_template(...)except TemplateNotFound:abort(404)

And then handle the 404 error with a custom error page as explained in the Flask documentation. Don't forget to import abort from flask and TemplateNotFound from jinja2

https://en.xdnf.cn/q/69334.html

Related Q&A

Injecting pre-trained word2vec vectors into TensorFlow seq2seq

I was trying to inject pretrained word2vec vectors into existing tensorflow seq2seq model.Following this answer, I produced the following code. But it doesnt seem to improve performance as it should, a…

MySQL Stored Procedures, Pandas, and Use multi=True when executing multiple statements

Note - as MaxU suggested below, the problem is specific to mysql.connector and does not occur if you use pymysql. Hope this saves someone else some headachesUsing Python, Pandas, and mySQL and cannot…

How can I change the font size in GTK?

Is there an easy way to change the font size of text elements in GTK? Right now the best I can do is do set_markup on a label, with something silly like:lbl.set_markup("<span font_desc=Tahoma …

How to read BigQuery table using python pipeline code in GCP Dataflow

Could someone please share syntax to read/write bigquery table in a pipeline written in python for GCP Dataflow

How can I wrap a python function in a way that works with with inspect.signature?

Some uncontroversial background experimentation up front: import inspectdef func(foo, bar):passprint(inspect.signature(func)) # Prints "(foo, bar)" like youd expectdef decorator(fn):def _wra…

Python OpenCV Error: TypeError: Image data cannot be converted to float

So I am trying to create a Python Program to detect similar details in two images using Pythons OpenCV. I have the two images and they are in my current directory, and they exist (see the code in line…

Specify timestamp on each packet in Scapy?

With Scapy, when I create a packet and write it to a pcap file, it sets the timestamp of the packet to the current time.This is my current usage. 1335494712.991895 being the time I created the packet:&…

Converting a dataframe to dictionary with multiple values

I have a dataframe likeSr.No ID A B C D1 Tom Earth English BMW2 Tom Mars Spanish BMW Green 3 Michael Mercury Hindi …

How do I create KeyPoints to compute SIFT?

I am using OpenCV-Python.I have identified corner points using cv2.cornerHarris. The output is of type dst.I need to compute SIFT features of the corner points. The input to sift.compute() has to be of…

Error in Tensorboards(PyTorch) add_graph

Im following this Pytorchs Tensorboard documentation. I have the following code: model = torchvision.models.resnet50(False) writer.add_graph(model)It throws the following error:_ = model(*args) # dont…