Answer the question
In order to leave comments, you need to log in
How to redirect from controller to controller?
I have a simple admin panel with registration and authentication. Authentication is in a separate auth_request() controller. I would like authentication to occur immediately after registration.
In order not to write repetitive code, I decided to redirect from the registration controller to the authentication controller. It is important that at the same time all the POST data transmitted through the registration form is saved. Here is my attempt:
@app.route('/auth_request', methods=['POST'])
def auth_request():
print('------------- auth req starts')
with DB_connection() as db_connect:
db_cursor = db_connect.cursor()
pasword = request.values.get('password')
password_hash = hashlib.sha1(pasword.encode('ASCII')).hexdigest()
email = request.values.get('email')
req = "select * from users where password_hash='" + password_hash + "' and email='" + email + "' and active=TRUE"
try:
db_cursor.execute(req)
user = db_cursor.fetchone()
resp = Response('authorized')
resp.headers['Set-Cookie'] = 'flask_adminka_authorized_user_id=' + str(user[0])
return resp
except:
resp = Response('not authorized')
return resp
@app.route('/registration_request', methods=['POST'])
def registration_request():
with DB_connection() as db_connect:
db_cursor = db_connect.cursor()
pasword = request.values.get('password')
password_hash = hashlib.sha1(pasword.encode('ASCII')).hexdigest()
email = request.values.get('email')
req = "insert into users(password_hash, email, active) values('" + password_hash + "', '" + email + "', 'TRUE')"
try:
db_cursor.execute(req)
redirect(url_for('auth_request'), code=307)
except:
resp = Response('registration is failed')
return resp
^C(flask_adminka) [email protected] ~/.MINT18/code/python/flask_adminka $ python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [07/Dec/2019 17:08:13] "GET /registration HTTP/1.1" 200 -
[2019-12-07 17:08:16,035] ERROR in app: Exception on /registration_request [POST]
Traceback (most recent call last):
File "/home/md/.local/share/virtualenvs/flask_adminka-0m3aMTkE/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/md/.local/share/virtualenvs/flask_adminka-0m3aMTkE/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
return self.finalize_request(rv)
File "/home/md/.local/share/virtualenvs/flask_adminka-0m3aMTkE/lib/python3.8/site-packages/flask/app.py", line 1967, in finalize_request
response = self.make_response(rv)
File "/home/md/.local/share/virtualenvs/flask_adminka-0m3aMTkE/lib/python3.8/site-packages/flask/app.py", line 2096, in make_response
raise TypeError(
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.
127.0.0.1 - - [07/Dec/2019 17:08:16] "POST /registration_request HTTP/1.1" 500 -
Answer the question
In order to leave comments, you need to log in
The flask wrote to you in Russian - "it would be nice to add return, host-ma."
try:
db_cursor.execute(req)
return redirect(url_for('auth_request'), code=307)
except:
return Response('registration is failed')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question