Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
page is not rendering after post request from ajaxtowards flask

I am using FLASK + Ajax to make login page. It should redirects to another page in case of correct username and password.

I made a form. and the request is passing successfully. and I can catch the data in python ( flask ). but my issue that no redirect happens after rendering the page. with render_template

Here is to redirect to login page when requesting the root url

@app.route('/')
def OperationPage():
    log_screenprint('User Has Requested / URL >> Redirect to /login')
    return  redirect(url_for('page_login'))

And this is the function which verify the username and password. then it should redirect to the next page

@app.route('/login' , methods=["POST","GET"])
def page_login() :
    log_screenprint('Got Request From GUI User to login With Cred.')

    for key,value in securityFile.items() :
        dbUser = securityFile[key]['username']
        dbpass = securityFile[key]['defaultpass']

        username = request.form.get('username')
        password = request.form.get('password')
        sessionId = request.form.get('session')

        if dbUser == username :
            if dbpass == password :
                socketio.emit('userDb' , json.dumps(securityFile[key]) , session = sessionId)
                return render_template('/index.html' ,  title= 'Home Page')

    return render_template('/login.html')

and this is the request I am using in javascript.

  $('#login-form').submit(function (e) {
  e.preventDefault();
  var username = document.querySelector('.login-username').value
  var password = document.querySelector('.login-password').value
    $.ajax({
      url: '/login',
      data: {
              username: username,
              password: password,
              session : (socket.id),
          },
        type: 'POST',
        success: function (response) {
          console.log('response');
        },
        error: function (error) {
          console.log(error);
        }
      });
  });

The issue again that. even the correct username and password. the index.html is not rendering

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

While using ajax you're in charge of what to do with server response. The response returned by server is html webpage (index.html) which you need to show for user. In your case you're just printing word 'response' to the webpage console:

success: function (response) {
  console.log('response');
},

I suggest doing it other way. I would create another route:

@app.route('/index')
def index():
    return render_template('/index.html' ,  title= 'Home Page')

and redirected user to it when server response would be successful.

success: function (response) {
  window.location.replace('index')
},

It also needs some changes for auhtentication function, since your server returns succesful http code every time. You can use something like:

return "", 200

for successful login, and

return "", 401

for failed.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!