5

I was trying to learn flask and came across the following problem.This is the example that i was trying to implement.

@app.route('/poll', methods = ['GET', 'POST'])
def poll():
    form = PollForm()

    if form.validate_on_submit():
        return render_template('details.html', form = form)

    return render_template('poll.html', form=form)

But i wanted to have a different url mapping for details.html, for that purpose i created another route as,

@app.route('/details/<form>')
def details(): 
   return render_template('details.html', form = form):

For using this i have used

return redirect(url_for('details', form=form))

in poll method inside the if condition. And when i tried to access the same from detail.html , i am not able to get it as a object. When tried to replace form with a string, it worked fine. Could you please suggest some mechanism to access form as an object inside the /details route ?

Edit

I was asking something like this is possible.

 @app.route('/poll', methods = ['GET', 'POST'])
    def poll():
        form = PollForm()

        if form.validate_on_submit():
        @app.route('/details')
            return render_template('details.html', form = form)

        return render_template('poll.html', form=form)

whenever we get inside the if condition the url will be /poll/details.Or is there any way to make this kind of url nesting, starting from a root url then child urls gets added depending on the business logic.

Tony
  • 1,120
  • 2
  • 11
  • 20

1 Answers1

8

You cannot just put a form object into a URL, no. A redirect() is a response, telling the browser to go load a different URL, the form object is not something you can easily cram into a URL path element.

If you don't need to see a different URL in the browser location bar, don't use a redirect but simply call a different function:

def details(form): 
   return render_template('details.html', form = form):

@app.route('/poll', methods = ['GET', 'POST'])
def poll():
    form = PollForm()

    if form.validate_on_submit():
        return details(form)

    return render_template('poll.html', form=form)

If you do need a different URL in the browser, then have the <form> element post to the /details route, not the /poll route.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • As you said it worked perfectly. Thanks for the answer. but i was wondering that something like nested URL routing is there in flask.Like in my case the if condition in the /poll can be linked to another route /details. So that the end url looks like /poll/details ? – Tony Jun 27 '14 at 19:55
  • @Tony: you can just register `/poll/details`; though I am not sure what exactly you are envisioning here. – Martijn Pieters Jun 27 '14 at 23:12
  • I have edited my question. Could you please check that and suggest me ? – Tony Jun 28 '14 at 06:59
  • Nope, still not cleAr what you are trying to achieve. Not that altering your question is the way to go about this; perhaps a chat with people in a chat room is the better approach for that kind of interaction. – Martijn Pieters Jun 28 '14 at 07:19