Z
Z
zlodiak2019-12-07 17:21:08
Python
zlodiak, 2019-12-07 17:21:08

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

As a result, after the user submits the registration form, the following error is displayed in the console:
^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 -

Please help me to make sure that authentication occurs after registration.
Full code is here .
PS:
Now authentication separately works without problems

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
antonksa, 2019-12-07
@antonksa

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')

redirect() is a FUNCTION that returns an object of CLASS Response , just specific, with code and headers. But he went into nihil, because you did not return him from the call. You could even shape it with your hands. Any code in the controller must eventually return a Response or throw an exception that the Flask can handle.
Also, you are NOT redirecting from controller to controller. You RETURN TO THE USER a response to the request, in which the redirect offer is packaged. He may have them disabled.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question