Click here to Skip to main content
15,937,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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.

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?

What I have tried:

chapters.html

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>


app.py

Python
from flask import Flask, render_template, request

app = Flask(__name__)

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

@app.route('/<chapter>', methods = ['POST', 'GET'])
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)
Posted
Updated 20-Sep-21 0:35am
v2
Comments
Richard MacCutchan 20-Sep-21 4:34am    
Check the actual url text that is generated by your code. The 405 error should also provide more information.
Thor Odinson 2021 20-Sep-21 5:20am    
The url is "127.0.0.1:5000" and it says "The method is not allowed for the requested URL."
Richard MacCutchan 20-Sep-21 5:43am    
That is not the complete URL sent to the server. You have chapters.html and questions.html in your code, so what is the complete text that is being sent?
Thor Odinson 2021 20-Sep-21 6:02am    
When we have "127.0.0.1:5000/chapters" (chapters.html), 5 buttons should be there and after clicking the first button the url should be "127.0.0.1:5000/chapter1" (for which the template is questions.html). But in this case, after clicking the first button, I'm getting the error. That's my question that where am I going wrong?
Richard MacCutchan 20-Sep-21 6:43am    
Well, whatever you are passing to the server is incorrect, so you need to check your server to see why. You can easily check the correct routes by typing the full URL into a browser tab.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900