0

My code ends by an error 405 even if I have allowed the GET method. When I am in localhost, it perfectly works, but I see this error in github page or pythonanywhere.

What could be the problem here ?

@app.route('/')
def my_home():
    return render_template('index.html')
  
@app.route('/submit', methods=['POST','GET'])
def submit():
      ytlink = request.form['yt link']
      lang = request.form['lang']
      Transcriptions = YouTubeTranscriptApi.get_transcript(ytlink, languages=[lang])
      formatter = TextFormatter()
      text_formatted = formatter.format_transcript(Transcriptions)
      return text_formatted

Here is some of the template :

<!DOCTYPE html>
<html>
    <body>
        <title>Youtube transcription</title>
        <h1>Download your transcription</h1>
        <form action="/submit" method='POST'>
            <label for="ytlink"><strong>YT id of the video:</label></strong>
            <input type="text" id="yt link" name="yt link">
            <br>
            <label for="language"><strong>Language:</label></strong>
            <input type="text" id="lang" name="lang">
            <br>
            <input type="submit" value="Click here to see your transcription", name='submit'>
        </form>

    </body>
</html>
AurelX
  • 1
  • 1

1 Answers1

0

Just tell the route when to use the specific methods.

@app.route('/submit', methods=['POST','GET'])
def submit():
    if request.method=='POST':
        ytlink = request.form['yt link']
        lang = request.form['lang']
        Transcriptions = YouTubeTranscriptApi.get_transcript(ytlink,   languages=[lang])
        formatter = TextFormatter()
        text_formatted = formatter.format_transcript(Transcriptions)
        return text_formatted

    else:
    #if you want anything to happen on the get request here.
rajat yadav
  • 374
  • 2
  • 7
  • It was my previous code and I had already this error. I changed after reading that : https://stackoverflow.com/questions/21689364/method-not-allowed-flask-error-405 – AurelX Jun 17 '21 at 06:06