`app.route` and `template_render` not working in Flask - python

I'm learning Flask and I created two routes :
"/about" : was routing a template as render.template(about.html)
"/aksh" : was routing a template as render.template(index.html)
It worked for index.html and not about.html and when I made changes in index.html and refreshed nothing happened.
I tried restarting everything including my system.
Then I commented that route "/aksh" but too much of my surprise portaddress/about was showing no url found whereas portaddress/aksh was still showing the text .
After that I re-edited whole code , now in new code there are 3 routes :
"/" : return a print statement "Hello World!"
"/aksh" : render a template index.html
"/about" : render a template about.html
But this has made no change now also I'm getting old results the new routes are not getting updated.
My python code is :
from flask import Flask,render_template
app=Flask(__name__)
#app.route("/")
def hello():
return "Hello World"
#app.route("/aksh")
def aksh():
return render_template('index.html')
#app.route("/about")
def about():
return render_template('about.html')
app.run(debug=True)
HTML codes for index and about are :
index.html :
<html>
<head>
<meta charset="utf-8">
<title>Title of my page</title>
</head>
<body>
<p>We are testing flask app here</p>
</body>
</html>
For about.html :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>About this page</title>
</head>
<body>
<h1>This is about section</h1>
</body>
</html>
Its showing "we are testing app" but it must be "we are testing app here " and moreover its in wrong location it must be in route "/aksh" not "/"

Use separators to solve this problem.
from flask import Flask,render_template
app=Flask(__name__)
#app.route("/")
def hello():
return "Hello World"
#app.route("/aksh")
def aksh():
return render_template('index.html')
#app.route("/about")
def about():
return render_template('about.html')
app.run(debug=True)

Finally I managed to solve this issue .
The problem was 1st I runned this code via PyCharm and then by Python interpreter IDLE (obviously after closing pyCharm) and somehow PyCharm was still controlling the server even after system restart .
So after checking everything and getting now clue I just powered off my system completely and started again and then run the application via IDLE only thus solving all issue !
So the conclusion was :
->PyCharm was still controlling the server even after system restart.
->Power off and on solved the issue for me .
Sounds weird but that's how it worked .
If anyone here can explain why PyCharm was still controlling servers even after restart would be better .

Related

render_template giving internal server error in aws beanstalk

I am trying to host a simple application in AWS elasticbeanstalk however when I am using render_template it is giving me this error "Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application."
Here is my flask code,
from flask import Flask, render_template
import requests
application = Flask(__name__, template_folder="templates", static_folder="static")
#application.route("/")
def index():
return render_template('index.html')
# run the app.
if __name__ == "__main__":
application.debug = True
application.run()
My HTML code,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div>
<h1>Hello, Flask!</h1>
</div>
</body>
</html>
My Dir Structure,
templates
|
------> index.html
application.py
requirments.txt
But When I dont use render_template i dont see any issue, Here is the code,
from flask import Flask, render_template
import requests
application = Flask(__name__, template_folder="templates", static_folder="static")
#application.route("/")
def index():
return "Hello Flask!!"
# run the app.
if __name__ == "__main__":
application.debug = True
application.run()
Thank you in advance.
Edit -
I was able to fix this issue, The problem was with the zip folder which I had created. Once I created a proper zip and uploaded it to aws beanstalk it started working.

Python 3.8.5 Flask giving internal server error with WSGI application when template is called using render_template in VS Code - 500 server error [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 2 years ago.
I have been following a YouTube tutorial made by Corey Schafer using Flask. I have reached the 2nd tutorial about using html templates, but that is the only place I have reached. This is the program I am running called hello.py:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
app.run()
This is the HTML file I have been using, called home.html:
<!DOCTYPE html>
<html>
<head>
<title>Flask Template Example</title>
</head>
<body>
<div>
<p>{{ message }}</p>
</div>
</body>
</html>
Whenever I try to run my code, I always get the error jinja2.exceptions.TemplateNotFound: template.html. I've tried to look at all possible solutions, but none have seemed to work. How could I fix this? I'm on a Windows 64-bit machine.
By default un Flask, the template folder is templates/. If home.html is in the same directory as app.py, you need to set template_folder.
Here is how to fix your app.py:
from flask import Flask, render_template
app = Flask(__name__, template_folder='./')
#app.route('/')
def home():
return render_template('home.html')
app.run()
To use the default template location (which is recommended), this is the file structure you would need to have:
app.py
templates
└── home.html

Got 404 error while using Flask framework

I was trying to run my html code using flask framework. When I tried to run the python script, it showed 404 error in the browser
<html>
<body>
<h1>Hello world!</h1>
</body>
</html>
python script:
from flask import Flask, render_template
app = Flask(__name__)
#app.route('/hello/<user>')
def hello_name(user):
return render_template('hello.html', name = user)
if __name__ == '__main__':
app.run(debug = True)
What is the reason of this error?
The code works fine.
Make sure that you keep the html file in templates folder. My folder structure is like this:
flask_app
├── hello.py
└── templates
└── hello.html
And also visiting the http://127.0.0.1:5000/ will cause the error as you did not define any view for root path.
While visiting the indexed path http://127.0.0.1:5000/hello/arsho I got the expected view:
Use jinja to print your name
<html>
<body>
<h1>Hello {{name}}</h1>
</body>
</html>
Go throught the flask quickstart

Generate pages with Flask without using separate template files

I'm trying to keep things minimal, so I don't want to create templates, directory structures, etc. I have a simple CSS file available on the web (via RawGit) and I want to use it to style the page generated by a view. How can I render a page without templates?
from flask import Flask
application = Flask(__name__)
# <insert magic here>
# application.get_and_use_my_super_cool_CSS_file(URL)
# <insert magic here>
#application.route("/")
def hello():
return "hello world"
if __name__ == "__main__":
application.run(host = "0.0.0.0")
If you don't want to write templates as separate files, you can use render_template_string instead. This is typically not a very useful feature, as it becomes difficult to maintain large templates written as Python strings. Flask's Jinja env also provides some extra help when rendering files it recognizes, such as turning on autoescape for HTML files, so you need to be more careful when rendering from strings directly.
return render_template_string('''<!doctype html>
<html>
<head>
<link rel="stylesheet" href="css url"/>
</head>
<body>
<p>Hello, World!</p>
</body>
</html>
'''

How to render index.html using python Flask MVC [duplicate]

This question already has answers here:
Flask raises TemplateNotFound error even though template file exists
(13 answers)
Closed 7 years ago.
This is absolutely new field for me, and I just confused about how this work
Flask server
$ more flask-hello-world.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return render_template('index.html') #"Hello World!"
if __name__ == "__main__":
app.run()
index.html
$ more index.html
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>untitled</title>
</head>
<body>
Hello worlds
</body>
</html>
Test
$ curl 127.0.0.1:5000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.</p>
When I return "Hello World!" it does work properly. Why am I getting an error when I try return render_template('index.html')?
First of all you should turn on debugging for your Flask app, so you see a meaningful error instead of a canned HTTP 500 Internal Server Error.
app.debug = True
app.run()
or
app.run(debug=True)
What could be wrong with your Flask app
From your source code I see you have not imported render_template
So that is at least one issue, fix it with:
from flask import Flask, render_template
# The rest of your file here
Template names and directory
There is a parameter template_folder
you can use to tell Flask where to look for your templates. According to linked documentation, that value defaults to templates which means Flask by default looks for templates in a folder called templates by default. So if your templates are on the project root instead of a folder, use:
E.g. if your templates are in the same directory is the app:
app = Flask(__name__, template_folder='.') # '.' means the current directory
You need to put your templates in templates folder, or ovride it in:
app = Flask(__name__, template_folder='.')

Categories

Resources