Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

Parametrizing routes in flask by multiple submit buttons

Sarge

New Coder
I am trying to make a website which has solutions of a book (5 chapters). Firstly, I want to open chapters.html with route /chapters which displays five submit buttons and chapter no. as their values.

[CODE lang="html" title="chapters.html"]<form action="/{{ chapter }}" method="POST">
<input type="submit" value="Chapter 1">
<input type="submit" value="Chapter 2">
<input type="submit" value="Chapter 3">
<input type="submit" value="Chapter 4">
<input type="submit" value="Chapter 5">
</form>
[/CODE]

And when the button is clicked, questions.html must be rendered with route as respective chapter number, like if "Chapter 2" is clicked, the route should be /chapter2. I have followed these SO posts Multiple submit Buttons in Flask and use many submit buttons in the same form to get some help.

[CODE lang="python" title="app.py"]from flask import Flask, render_template, request

app = Flask(__name__)

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

@app.route('/<chapter>')
def questions(chapter):
if 'Chapter 1' in request.form:
chapter = 'chapter1'
elif 'Chapter 2' in request.form:
chapter = 'chapter2'
elif 'Chapter 3' in request.form:
chapter = 'chapter3'
elif 'Chapter 4' in request.form:
chapter = 'chapter4'
elif 'Chapter 5' in request.form:
chapter = 'chapter5'
return render_template('questions.html', chapter = chapter)

if __name__ == '__main__':
app.run(debug = True)[/CODE]

I am getting Error 405 (Method not allowed) when I click on chapters button but it works well when I manually type the route /chapter1 or /chapter5. How can I solve this problem? Also, do I require any AJAX script to do this?
 
This is overly complicated. Why not use simple anchors to do this? That's their job.

<a href="https://www.chapter_1">Chapter 1</a>
and so on. I personally would use the complete address to the page.
Add the styling you like.
 
This is overly complicated. Why not use simple anchors to do this? That's their job.

<a href="https://www.chapter_1">Chapter 1</a>
and so on. I personally would use the complete address to the page.
Add the styling you like.
Actually, by buttons I can get a unique id (value attribute) for each url which can be used to pass some data from another system (probably pull data from database). By anchoring I may not be able to get the id. Is there a way I can get it from the url? I just to make dynamic url for each button and not hard code route for each.
Also, can you please tell me how can I edit my question, there doesn't seem to be any option.
 
Actually, by buttons I can get a unique id (value attribute) for each url which can be used to pass some data from another system (probably pull data from database).
You don't show sending an 'ID' in your example. If you would send total code - or how you would get that ID I'd show you how to do it.

My question to you is "Are these ID's unique to one chapter or used by all chapters?".
 
You don't show sending an 'ID' in your example. If you would send total code - or how you would get that ID I'd show you how to do it.

My question to you is "Are these ID's unique to one chapter or used by all chapters?".
The value of the value attribute is the id which I'm retrieving by request.form
 
You are not making sense. HTML can not process a form. You have a form that the user picks an ID - that has to go server side and you probably use ASP to process that. Why don't you have the user pick the chapter in the same form?

If you come up with a reason "why not" Then have the ASP generate the HTML you posted in post 1 and add the ID to it as a hidden field.
 

New Threads

Buy us a coffee!

Back
Top Bottom