Getting a 500 Internal Server Error using render_template and Flask [duplicate]

2024/10/12 2:24:14

I am trying to use Flask to render an HTML template. I had it working great and now each time I get a 500 Internal Server Error. If I replace the render_template function just a string, things work fine. What am I doing wrong?

init.py :

from flask import Flask, render_templateapp = Flask(__name__)@app.route('/')
def homepage():return render_template("main.html")if __name__ == "__main__":app.run()

main.html in /templates/

<!DOCTYPE html>
<html lang="en">
<p>test</p>
</html>
Answer

The template_folder has to be defined where the static files are located.

If the main.html file is in the same folder as the init.py, then include the following code:

import osproject_root = os.path.dirname(__file__)
template_path = os.path.join(project_root, './')app = Flask(__name__, template_folder=template_path)

Hopefully it works now.

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

Related Q&A

Bokeh use of Column Data Source and Box_Select

Im lost as to how to set up a Column Data Source so that I can select points from one graph and have the corresponding points highlighted in another graph. I am trying to learn more about how this work…

How Does a Pyqtgraph Export for Three Subplots Look Like?

Using PyQtGraph, I would like to generate three sub plots in one chart and export this chart to a file.As I will repeat this a lot of times, it is quite performance sensitive. Therefore I do not need t…

Use class variables as instance vars?

What I would like to do there is declaring class variables, but actually use them as vars of the instance. I have a class Field and a class Thing, like this:class Field(object):def __set__(self, instan…

Get amount from django-paypal

I am using django-paypal to receive payment. I am currently paying as well as receiving payment using sandbox accounts. The payment procedure seems to be working fine. My problem is once I get back the…

Python get file regardless of upper or lower

Im trying to use this on my program to get an mp3 file regardless of case, and Ive this code:import glob import fnmatch, redef custom_song(name):for song in re.compile(fnmatch.translate(glob.glob("…

how to save h5py arrays with different sizes?

I am referring this question to this. I am making this new thread because I did not really understand the answer given there and hopefully there is someone who could explain it more to me. Basically my…

Cannot allocate memory on Popen commands

I have a VPS server with Ubuntu 11.10 64bit and sometimes when I execute a subprocess.Popen command I get am getting too much this error:OSError: [Errno 12] Cannot allocate memoryConfig details: For ea…

Python - find where the plot crosses the axhline on python plot

I am doing some analysis on some simple data, and I am trying to plot auto-correlation and partial auto-correlation. Using these plots, I am trying to find the P and Q value to plot in my ARIMA model.I…

remove tick labels in Python but keep gridlines

I have a Python script which is producing a plot consisting of 3 subplots all in 1 column.In the middle subplot, I currently have gridlines, but I want to remove the x axis tick labels.I have triedax2.…

Signal in PySide not emitted when called by a timer

I need to emit a signal periodically. A timer executes certain function, which emits the signal that I want. For some reason this function is not being emitted. I was able to reproduce the error on min…