I have noticed that after a POST, if you look at the page source via "control + U" flask opens the source page also again via POST and does all tasks again. Is there any way I can prevent this?
#app.route("/", methods=["POST", "GET"])
def event_page():
if request.method == "POST":
do_something()
when I view the source page the do_something() function is trigger t again.
Not really a Flask problem. The simplest solutions would be not to use CTRL+U but F12 - inspector or network tab
Related
I am trying to redirect to the login page once the session ends, but since I'm using extended HTML template for my dashboard what happens while redirecting though is that the app renders the login page within the sections of the dashboard instead of completely redirecting to the login page, I also looked up for how reload the page by itself using flask yet couldnt find any possible solutions.
This is what is happening
The code snippet
session['user'] = form.username.data
print(session['user'],']')
session.permanent = True
app.permanent_session_lifetime = timedelta(seconds=5)
session.modified = True
return redirect(url_for('signup'))
All I want to do is redirect to Login once the session ends
Although I couldn't find a proper solution to the above, achieved what I wanted to by using the below code:
return '<script>window.location = "/login";</script>'
Since this might help someone who wants a fix to the same.
I am trying to log all activity on my Flask web app using the flask logger. I know how to get logs of application errors as is mentioned in this link
http://flask.pocoo.org/docs/1.0/logging/#logging
The problem with this is that it only returns logs for the first time that i go to the local server to launch the app. No logs are returned after that. Logs are only updated if i change something in my code but not when i log in to the app or go to different tabs within the app. What is the procedure If I want to get the logs for activity within the app?
#app.route("/cis", methods=['GET','POST'])
#login_required
def main():
form = ReusableForm(request.form)
if request.method == 'POST':
if form.validate():
try :
app.logger.info("Reached /cis")
else:
return render_template('cis.html',form=)
For example if i want to show "Reached /cis" in the logs when i visit "/cis" in my app what should be done?
I am having an issue with my fading modal routing in flask. My user login opens a modal and i m trying to implement the POST feature from the modal in flask.
I thought of implementing under index and search for the form name form the the post like below.
#app.route('/')
#app.route('/index')
def index():
if request.form.get('login', None) == 'submit' :
return 'Yeah hooo'
return render_template('index.html')
However, when i execute the code above, i get
Method Not Allowed
on /index. My other worries is that my Login form is in the template and can therefore be call from any routes. Since template is not a route. Please is there any way i can achieve this in flask ? Or do i have to take the login in to a seperate html file instead of the template ?
You should explicitly add POST to the list of methods that can be processed
#app.route('/', methods=['GET', 'POST'])
See here for more information
As per the second question, it's ok as long as your login form makes POST request to the same route ('/' in your case).
I have a very simple python program using Flask shown below. It handles a login with a popup and logout. The problem is that the url in the browser is not updated by the redirect(url_for()) call.
#app.route('/')
def index():
if not 'username' in session:
# contains a button showing a login popup form with action set to '/login'
return render_template('welcome.html')
else:
# contains a logout button with a href to '/logout'
return render_template('webapp.html')
#app.route('/login', methods=['POST'])
def login():
session['username'] = request.form['username']
return redirect(url_for('index'))
#app.route('/logout')
def logout():
session.pop('username', None)
return redirect(url_for('index'))
When accessing '/' the welcome page is shown. When I click on the button, the login popup is shown and its form action redirects to '/login'. This works and the login() function is called and executed. The redirect as well, but the browser doesn't update the displayed url.
So the webapp page is shown with the /logon url. When I click reload I get an error because it tries to reload /logon while it should reload '/' where it has been redirected.
The same happens with /logout. When the webapp page is shown and I click the logout button, the /logout page is loaded which executes the logout() function and redirects to index. But the url is left to logout.
If I then reload the page, it succeeds because /logout accept the GET method and then the url is updated to / as it should have been in the first place.
I have the impression it is a jQuery mobile issue, but can't find out the problem. From the python and Flask point of view it matches all login examples I could find.
Finally solved it after finishing writing the question.
The problem is caused by jQuery mobile and the missing data-url attribute.
By adding the data-url attribute in the page div the url in the browser is updated and everything works fine.
<div data-role="page" id="welcome" data-url="{{ url_for('index') }}">
from bottle import route, run, debug, error, request, template
#route('/home')
#route('/home/')
def login():
return template('templates/login')
#route('/home', method='POST')
#route('/home/', method='POST')
def welocme():
data = request.POST
if data:
password = data.get('password')
check_pass = 'password'
if password == check_pass:
return template('templates/welcome')
else:
return template('templates/login')
else:
return template('templates/login')
My requirement is:
I will get a login and welcome page on same url. Login page has only one password field.
My problem:
If I get login and go to welcome page again on refreshing, it send me to login page. But ideally it should be on the welcome page only.
#error(404)
def error404(error):
return 'http://google.com'
My second problem: I want to redirect on a particular url on 404.
If the user goes to the "/home" page, don't you want to check to see if they're logged in before showing them the login screen? It appears like you assume they're not already logged in if the HTTP method is not POST.
I don't know much about your framework, but I assume if the login is successful you should set a cookie, and then you should check the cookie for HTTP GETs to see if the user is authenticated.
Your second question is answered here.
You might also want to take a look at Beaker's cookie sessions and use that to keep your application's state between requests.
If I understand the question correctly, the only way to get to render the welcome template is via POST.
You could change this so that GET requests check whether someone is logged in. If that fails, then redirect them to the login page.