0

I am working with a Flask template to create a table where the rows are populated by a database. I am attempting to edit one of these rows when its respective edit button is clicked. Currently, the code iterates through database results and populates the table, attaching a button to each row. When the button is clicked a script is called that uses ajax to call my python function that is supposed to handle the input and redirect the page to edit.html. Everything gets called properly, but the page just refreshes and stays on tables.html and never leaves. Attached below is the code

@app.route('/tables.html', methods=['GET','POST'])
def questions():
   table = list(getAllQuestions())
   if(request.method == 'GET'):
       return render_template('layouts/default.html',
                              content=render_template( 'pages/tables.html', value=table,), value=table)

def getAllQuestions():
   conn = mysql.connect()
   cursor = conn.cursor()
   cursor.execute("SELECT * FROM tbl_questions")
   data = cursor.fetchall()
   data = data[:10]
   return data
@app.route("/edit.html", methods=['GET', 'POST'])
def edit():
   print("in edit")
   form = RegisterForm(request.form)
   return render_template('pages/edit.html', form=form)
@app.route('/background_process_test', methods=["GET", "POST"])
def background_process_test():
   return redirect(url_for('edit'))

the html and js

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type=text/javascript>
function clickfunction(value) {
console.log(value)
var resp = {
    'value': value
    }
console.log(resp)

$.ajax({
url: "/background_process_test",
type: 'POST',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(value),
dataType: 'json',
success: function(response) { alert(response); }
})
}
</script>

      <div class="panel-header panel-header-sm">
      </div>
      <div class="content">
        <div class="row">
          <div class="col-md-12">
            <div class="card">
              <div class="card-header">
                <h4 class="card-title"> Questions </h4>
              </div>
              <div class="card-body">
                <div class="table-responsive">
                  <table class="table">
                    <thead class=" text-primary">
                      <th>
                       Question
                      </th>
                      <th>
                       Round
                      </th>
                    </thead>
                    <tbody>
                      {% for row in value %}
                         <tr id="table">
                           <td>{{row[0]}}</td>
                            <td>{{row[1]}}</td>
                            <td>{{row[2]}}</td>
                             <td><form><a><button class='editButtons' value={{row[0]}} onclick="clickfunction({{ row[0] }})">Edit</button></a></form> </td>
                         </tr>

                      {% endfor %}
                    </tbody>
                  </table>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

The respective output is

in edit
127.0.0.1 - - [22/Mar/2020 12:58:03] "GET /edit.html HTTP/1.1" 200 -
127.0.0.1 - - [22/Mar/2020 12:58:04] "GET /tables.html HTTP/1.1" 200 -

When all this gets run, "in edit" does get logged to the console, but the page is never rendered. Instead, the tables page is just refreshed and nothing seems to happen as you can tell by the GET request after the one to edit. I am sure I am making some dumb mistake but this is my first flask project (and honestly first web project as well, never used js). If anything needs to be clarified or if more code is needed to understand the circumstances surrounding the issue, let me know and I will clarify

stigward
  • 127
  • 13
  • Haven't read entirely, but can you try this: `redirect(url_for('edit'), 307)` ? (notice the 307 code asking the browser to redirect) – Rahul Bharadwaj Mar 22 '20 at 17:07
  • @RahulBharadwaj Same issue. Not sure why – stigward Mar 22 '20 at 17:24
  • I see, maybe its because redirecting the entire page via Ajax is not that straightforward (as oppsed to a form based redirect). I found this answer, see if its helpful: https://stackoverflow.com/a/484541/6400614 – Rahul Bharadwaj Mar 22 '20 at 17:28

0 Answers0