简体   繁体   中英

flask not redirecting to new page after redirect(url_for())

I have a simple app that needs to run a subprocess.call in celery and simply return some text to the screen as it's been successfully submitted.

Try as I might, I have been unable to make the redirect(url_for()) do anything. I see in the log that I get a 302 and a 200 but no new page in the browser window.

Here is my the pertinent part of my server.py code:

#!/usr/bin/python

import os
from flask import Flask, url_for, jsonify, request, make_response, render_template, json, flash, Markup, redirect
from celery import Celery, states
from flask_cors import CORS, cross_origin
import pandas as pd
import subprocess

app = Flask(__name__)
CORS(app)
app.secret_key = 'random string'
app.config['CELERY_BROKER_URL'] = 'amqp://cluster:cluster@localhost/myvhost'
app.config['CELERY_RESULT_BACKEND'] = 'amqp://cluster:cluster@localhost/myvhost'
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)


@app.route('/')
def main():
    return render_template('index.html')

@app.route('/showPrevNext')
def showPrevNext():
    return render_template('prevnext.html')

@app.route('/PrevNext',methods=['GET','POST'])
def prevNext():
    _uid      = request.form['inputUid']
    _db   = request.form['inputName']
    _TableName = request.form['inputTableName']
    if  _id:
        uid = _uid
        if _db:
            db = _db
            if _TableName:
                TableName = _TableName
                flash("Running  images!")
                return redirect(url_for('main'))
                create_some_stuff.delay(uid,db,TableName)

create_some_stuff is a celery task that runs fine but I am having trouble making the redirect work. After many hours of toying, I hope one of you can help please?

output from the console is:

 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin code: 270-380-963
192.168.200.238 - - [15/Feb/2018 16:55:07] "POST /PrevNext HTTP/1.1" 302 -
192.168.200.238 - - [15/Feb/2018 16:55:08] "GET / HTTP/1.1" 200 -

Am I missing something simple?

Thanks

I am having trouble making the redirect work.

Maybe conditional expressions in prevNext doesn't lead to functions return. Try to move return to the first level and see what will happen. Like so:

@app.route('/PrevNext',methods=['GET','POST'])
def prevNext():
    _uid      = request.form['inputUid']
    _db   = request.form['inputName']
    _TableName = request.form['inputTableName']
    if  _id:
        uid = _uid
        if _db:
            db = _db
            if _TableName:
                TableName = _TableName
                flash("Running  images!")
                create_some_stuff.delay(uid,db,TableName)
    return redirect(url_for('main')) #moved

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM