POST Method not allowed for a simple lookup webpage

Viewed 30

I have a simple page with a data entry field and a click button, this will run the API to retrieve the coin data

running the code in a python terminal return with success, but when I try to add it to flask and use the webpage, I get the error 405 method not allowed for the POST.

This is the main python/flask file: crypto.py

# template libraries
from flask import render_template,url_for,flash,request,redirect,Blueprint

# Coingecko API library
from pycoingecko import CoinGeckoAPI

crypto_simulator = Blueprint('crypto_simulator',__name__)

@crypto_simulator.route('/crypto_simulator', methods=['GET','POST'])
@login_required
def crypto_insert():
    if request.form.get("ident") == "formCrypto":
        print('Hello')

        cg = CoinGeckoAPI()

        #crypto_token = request.form.get('crypto_name_html', '')
        crypto_token = 'bitcoin'
        crypto_currency = 'usd'

        response = cg.get_price(ids=crypto_token,
                                vs_currencies='usd', 
                                include_market_cap='true', 
                                include_24hr_vol='true',
                                include_24hr_change='true',
                                include_last_updated_at='true')

        crypto_result = response.get(crypto_token,'')
        print(crypto_result[crypto_currency])

        return render_template('crypto_simulator.html',
                                formCryptoSimulation=form,
                                crypto_token=crypto_token,
                                crypto_currency=crypto_currency,
                                crypto_result=crypto_result
                                )

This is the Blueprint routing file: core.py

# crypto section
@core.route('/crypto_simulator')
def crypto_simulator():
    return render_template('crypto_simulator.html')

This is the Flask/Bootstrap front-end: crypto_simulator.html

{% extends "base.html" %}
{% block content %}

<!-- Simulation Code Start -->
<div class="forms">
    <div class="formCrypto">
        <form method="post" action="{{ url_for('core.crypto_simulator') }}">
          <div class="container">
            <div class="row g-3">
            <div class="col-sm-3">
                <label class="form-label"><b>Crypto Name:</b></label>
                <input type="text" class="form-control" name="crypto_name_html" placeholder="Enter Crypto Name" required>
            </div>
          </div>
        </div>
            <br>
            <div class="d-grid gap-2 d-md-flex justify-content-md-start">
              <button id="btn" type="submit" class="btn btn-info">Check Token!</button>
            </div>
            <input type=hidden name="ident" value="formCrypto">
        </form>
        <br>
            <p>Token: <b>{{crypto_token}}</b>
            <p>Price: <b>{{crypto_result}}</b>
    </div>
</div>

{% endblock %}

I checked for misspelled lines and anything related but still stuck into how to fix it...

1 Answers

Your form has this:

<form method="post" action="{{ url_for('core.crypto_simulator') }}">

So you are calling the function crypto_simulator in blueprint core:

@core.route('/crypto_simulator')
def crypto_simulator():
    return render_template('crypto_simulator.html')

Note that your form does a POST request, so you very logically have to enable the POST method on the function being called like this:

@core.route('/crypto_simulator', methods=['GET','POST'])
Related