1

I'm pretty new to python flask, Just wanted to check my code below, where I'm doing things wrong.

As when I'm running when on URL like (localhost:5000/submit?name=dial&id=565337) it's running properly, though it's not running when I'm passing values on the form and producing an error.

from flask import Flask, request, redirect, url_for
import Eoc_Summary
import Eoc_Daily
import Eoc_AdSize
import Eoc_Video
import Eoc_Intraction
import EOC_definition
from config import Config

app = Flask(__name__)

form = '''
<html>
   <body>
      <form action = "http://localhost:5000" method="POST">
         <p>Enter Name:</p>
         <p><input type = "text" name = "name" /></p>
         <p>Enter id:</p>
         <p><input type = "text" name = "id" /></p>
         <p><input type = "submit" value = "submit" /></p>
      </form>
   </body>
</html>
'''

@app.route("/")
def index():
    if request.method == 'GET':
        return form
    elif request.method == 'POST':
        name = request.form['name']
        id = request.form['id']
        return submit(name, id)

@app.route('/submit')
def submit():
    name = request.args.get('name')
    id = request.args.get('id')
    c = Config(name, int(id))

    obj_summary=Eoc_Summary.Summary(c)
    obj_summary.main()
    obj_daily=Eoc_Daily.Daily(c)
    obj_daily.main()
    obj_adSize=Eoc_AdSize.ad_Size(c)
    obj_adSize.main()
    obj_Video=Eoc_Video.Video(c)
    obj_Video.main()
    obj_Intraction=Eoc_Intraction.Intraction(c)
    obj_Intraction.main()
    obj_definition=EOC_definition.definition(c)
    obj_definition.main()
    c.saveAndCloseWriter()
    return 'Report Generated'


if __name__ == '__main__':
    app.run()
5
  • Try putting a slash after localhost:5000 like this: localhost:5000/ in the form action Commented Feb 21, 2018 at 20:13
  • @TimLee Thanks for your response however still getting error "Method Not Allowed" The method is not allowed for the requested URL. Commented Feb 21, 2018 at 20:17
  • 1
    next to @app.route("/") put methods =['GET','POST] like so: @app.route("/", methods =['GET','POST]) This lets flask know that the get and post methods are coming in. Commented Feb 21, 2018 at 20:19
  • Now it's giving internal server error Commented Feb 21, 2018 at 20:24
  • Send the post request to /submit by changing the form action address and specifying methods=[‘POST’] in /submit routing. Also, at the end of submit routing, return a redirect by putting redirect(‘http:/localhost:5000/‘) you will have to import redirect from flask. Commented Feb 21, 2018 at 20:54

1 Answer 1

1

You have to add the methods in your decorator

@app.route("/", methods=['GET', 'POST'])
def index():
   {...}

Also you'll have to add your arguments name and id to submit():

@app.route('/submit')
def submit(name, id):
   {...}

Finally, import make_response

from flask import Flask, request, redirect, url_for, make_response
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks for the response, however, its not working giving an error.
You have to import make_response as from flask import Flask, request, redirect, url_for, make_response
You also need to add the arguments for submit(). I edited my reply.
Now its giving this error. " c = Config(name,int(id)) TypeError: int() argument must be a string or a number, not 'NoneType'"
What value did you input for id ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.