Skip to content

Instantly share code, notes, and snippets.

@TheMuellenator
Forked from angelabauer/main.py
Last active December 29, 2023 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save TheMuellenator/29acd670326c6313bc2b8f7e66fd441f to your computer and use it in GitHub Desktop.
Save TheMuellenator/29acd670326c6313bc2b8f7e66fd441f to your computer and use it in GitHub Desktop.
Day 61 L464 - Installing Flask-WTF
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def home():
return render_template('index.html')
@app.route("/login")
def login():
return render_template("login.html")
if __name__ == '__main__':
app.run(debug=True)
@dorterry
Copy link

dorterry commented Aug 1, 2022

WOW - that requirement.txt file blew up ALL my Pycharm projects where I kept getting the same ImportError: cannot import name 'Markup' from 'jinja2' for all my projects. I'm sure I clicked on the wrong thing at some point. So after a little bit of a panic lol, this is what fixed it for me as of 8/1/22.

Uninstall Flask and Jinja
Open terminal and type:
pip uninstall Flask Jinja2

Reinstall latest version of Flask and Jinja
after that finishes type:
pip install Flask Jinja2

That fixes the big blowup problem and then as others have said, you also have to edit the requirements.txt file:
Flask==2.1.3
itsdangerous==2.1.2

and then finally update main.py with the /login route

from flask import Flask, render_template

app = Flask(name)

@app.route("/")
def home():
return render_template('index.html')

@app.route("/login")
def login():
return render_template('login.html')

if name == 'main':
app.run(debug=True)

@geba89
Copy link

geba89 commented Aug 19, 2022

For anyone who is struggling, here is HTML code:

<form method="get" action="{{ url_for('login_page') }}">
 	<button class="btn btn-primary btn-lg" type="submit">Login</button>
 </form>	
 

You need to add FORM tag set ACTION for it and make button as type submit.

@andriigorshunov
Copy link

I did not click on this yellow bar on with Install requrement for Flask ==1.0.2 and code worked. When I click on it stops to work.

@fernandoaleman
Copy link

WOW - that requirement.txt file blew up ALL my Pycharm projects where I kept getting the same ImportError: cannot import name 'Markup' from 'jinja2' for all my projects. I'm sure I clicked on the wrong thing at some point. So after a little bit of a panic lol, this is what fixed it for me as of 8/1/22.

Uninstall Flask and Jinja Open terminal and type: pip uninstall Flask Jinja2

Reinstall latest version of Flask and Jinja after that finishes type: pip install Flask Jinja2

That fixes the big blowup problem and then as others have said, you also have to edit the requirements.txt file: Flask==2.1.3 itsdangerous==2.1.2

and then finally update main.py with the /login route

from flask import Flask, render_template

app = Flask(name)

@app.route("/") def home(): return render_template('index.html')

@app.route("/login") def login(): return render_template('login.html')

if name == 'main': app.run(debug=True)

@dorterry Thanks, this fixed it for me 👍

@Olushola11
Copy link

In my case, I needed to update the requirements.txt file accordingly

Flask==2.0.3' 'itsdangerous==2.1.0' 'flask

thanks

@Cgorbenko
Copy link

Login

Thanks!

@Berkayclk06
Copy link

Hi there!

If anyone gets an error while starting the project, just make these changes to requirements.txt

Flask==2.0.3
Jinja2==3.1.1
itsdangerous==2.0.1
flask

@MPoblocka
Copy link

MPoblocka commented Oct 5, 2022

Still not working :( I did as you said:
Flask==2.0.3
Jinja2==3.1.1
itsdangerous==2.0.1

flask

and also updated button link:
'< a href= "{{ url_for('login') }} ">Login

Still not working for me. The Error I am getting is:
from flask import Flask, render_template
ModuleNotFoundError: No module named 'flask'

@brunozebendo
Copy link

Flask==2.1.3
itsdangerous==2.1.2

Thank you, after one hour trying to figure it out, finally worked with your tips...

@akash-siv
Copy link

As @lukasm1 said, I had the same problem. The link provided by @lukasm1 doesn't work for me. For people who are facing the same problem, I used <a href=" {{ url_for('login') }} "> <button class= "btn btn-primary btn-lg">Login</button></a>. I think it's actually a link that looks like a button and it seems to work.

thank you this works for me..😍

@Chyngyz-UB
Copy link

itsdangerous==2.1.2

Thanks a lot! It worked!

@clexp
Copy link

clexp commented Nov 4, 2022

Could someone comment on the following: if you needed to change the requirements file, how did you know you needed to? and how did you find out what to change it to?

Course tutor indicated the requirements file was important and linked a bunch in the previous section, but I did not need to touch it. I followed the links and read some of it but didn't understand any of it. This stuff really is written for developers from other frameworks, not new starters.

@andriigorshunov
Copy link

Could someone comment on the following: if you needed to change the requirements file, how did you know you needed to? and how did you find out what to change it to?

Course tutor indicated the requirements file was important and linked a bunch in the previous section, but I did not need to touch it. I followed the links and read some of it but didn't understand any of it. This stuff really is written for developers from other frameworks, not new starters.

requirements.txt is a file located in a root folder of your project and contains info about what packages are required for your project to run successfully. Usually it is used, when you migrating a project or deploying a project on web-server, so server will know what to download and then run your app. To see what packages are installed you can by doing in terminal "pip freeze", in order to save all packages currently used as dependencies into requirements.txt file you need to run a terminal command "pip freeze > requirements.txt". You can also see packages used in project via Pycharm > File > Settings > Project: "your project name" > Python Interpreter . You will same list of all installed packages.

Once program works, it is good to save a state what versions are used to requirements.txt, because in future with new versions will be released of any package (flask, pandas, etc) it may stop to work due to new syntax or deprecated functions and will require code adjustment. Thus old trusted and checked version that was frozen - will still work.

@OrawusiTimilehin
Copy link

ImportError: cannot import name 'Markup' from 'jinja2' for me and I cant solve it..

You have to uninstall and Install jinja again

https://stackoverflow.com/questions/71645272/importerror-cannot-import-name-markup-from-jinja2

@kakaa2993
Copy link

You don't need all of that , we just need the main.py and the templates file.
in the index.html

<!DOCTYPE HTML>
<html>
<head>
	<title>Secrets</title>
</head>
<body>
<div class="jumbotron">
	<div class="container">
		<h1>Welcome</h1>
		<p>Are you ready to discover my secret?</p>
		<form action="/login" method="get">
			<button class="btn btn-primary btn-lg" href=" {{ url_for('login') }} " >Login</button>
		</form>
	</div>
</div>
</body>
</html>

and for the main.py :

from flask import Flask, render_template

app = Flask(__name__)


@app.route("/")
def home():
    return render_template('index.html')


@app.route("/login")
def login():
    return render_template("login.html")


if __name__ == '__main__':
    app.run(debug=True)

that's what works for me :)

@Nagham2022
Copy link

Nagham2022 commented Nov 24, 2022

I followed these steps:

1- Open Terminal and type:
pip uninstall Flask Jinja2
pip install Flask Jinja2

2- Update the requirements.txt file:
Flask==2.0.3
Jinja2==3.1.1
itsdangerous==2.0.1
flask

3- Update main.py with the /login route as shown in the solution

4- Update index.html :

..code before...

<p>Are you ready to discover my secret?</p>
	<form action="/login" method="get">
	      <button class="btn btn-primary btn-lg" href=" {{ url_for('login') }} ">Login</button>
	</form>

...code after..

5- Close the terminal and click terminate then open it again and write:
set FLASK_APP=main.py
$env:FLASK_APP="main.py"
python -m flask run

then it will run and gives you the link but If you get the link of the previous application so again close the terminal and open it and write:

set FLASK_APP=main.py
$env:FLASK_APP="main.py"
python -m flask run --host 0.0.0.0 --port 5001

@arad1367
Copy link

arad1367 commented Dec 4, 2022

For people who see this part in December 2022 and receive error : ImportError: cannot import name 'Markup' from 'jinja2', put below codes inside of requirement.txt. Good luck!.

click==8.0.4
Flask==2.0.3
itsdangerous==2.1.2
Jinja2==3.1.1
MarkupSafe==2.1.1
Werkzeug==2.0.3

@EugeneYe1
Copy link

Flask==2.2.2
itsdangerous==2.1.2
flask

Copy and paste that into the requirements.txt and you should be prompted to install the plugin in the main.py python file.

@idan6767
Copy link

idan6767 commented Feb 3, 2023

so there is a problem with how the login button behave.
aperrantly "button" tags dont accept href, to fix this all you neeed to do is to seperate them, like that:

	<a href="{{ url_for('login') }}">
            <button class="btn btn-primary btn-lg">Login</button>
            </a>

@abeyasmare
Copy link

abeyasmare commented Feb 26, 2023

For all your problems for this projects

  1. for jinja and flask related problems simply do pip uninstall jinja2 flask after that do pip install jinja2 flask that will work just fine but if it dont make sure the requirements file has the latest dependencies
  2. for the button not working, button tag doesnot have href attribute, it may work if you add onclick listener (it is javaScript code) but buttons are interactive and it is activated when user interacts with them. so when they are activated it performs an action. the actions can be js code (event listeners) or it can be activated using form tag that has action attribute and specifing the type of requests using method

@cipa5
Copy link

cipa5 commented Apr 10, 2023

ImportError: cannot import name 'Markup' from 'jinja2' for me and I cant solve it..

You need to install Flask==2.2.3. To do this first 1) pip install --upgrade -pip, then 2) uninstall current flask and jinja using pip uninstall Flask Jinja2, 3) install them again using pip install Flask Jinja2, finally 4) utilize pip freeze to check the current state of environment packages. At this moment this looks like this to me but it might change in the future depending on the version of the specific package:
click==8.1.3
Flask==1.0.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
Werkzeug==2.2.3
Of course all this commands have to be executed in Terminal

@shyam3017
Copy link

why the login button is not working?

@shyam3017
Copy link

so there is a problem with how the login button behave. aperrantly "button" tags dont accept href, to fix this all you neeed to do is to seperate them, like that:

	<a href="{{ url_for('login') }}">
            <button class="btn btn-primary btn-lg">Login</button>
            </a>
`This works thank you

@arpy8
Copy link

arpy8 commented May 7, 2023

ImportError: cannot import name 'Markup' from 'jinja2' for me and I cant solve it..

Can any one help me, how to resolve this problem

https://stackoverflow.com/questions/71645272/importerror-cannot-import-name-markup-from-jinja2

this might help you

@parvathyhydro
Copy link

Hi there!

If anyone gets an error while starting the project, just make these changes to requirements.txt

Flask==2.0.3
Jinja2==3.1.1
itsdangerous==2.0.1
flask

@parvathyhydro
Copy link

Thank you so much it help me out .

@Wrinklytech
Copy link

so there is a problem with how the login button behave. aperrantly "button" tags dont accept href, to fix this all you neeed to do is to seperate them, like that:

	<a href="{{ url_for('login') }}">
            <button class="btn btn-primary btn-lg">Login</button>
            </a>
`This works thank you
<button onclick="window.location.href='{{ url_for('login') }}';">Login</button>

This also seems to work.

@bahae3
Copy link

bahae3 commented Jun 11, 2023

I received this: from jinja2 import Markup, escape ImportError: cannot import name 'Markup' from 'jinja2'

Tried the methods above but it did not work. What should I do?

Hi,
try this on your terminal: pip install --upgrade flask jinja2

@gureumjigi
Copy link

For everyone who suffers from ImportError:
type this code below in your Terminal.

pip install Flask==2.0.3

This worked for me. Good Luck!

@inavadeep1205
Copy link

app = Flask(__name__)

@app.route("/")
def login():
    return render_template("login.html")

app.run()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment