I am getting this error:
"The method is not allowed for the requested URL" when i try to submit a request.
Here's my python code:
from flask import Flask,render_template,request
import requests
api_address='http://api.openweathermap.org/data/2.5/weather?appid=014093f04f1d04c9e512539a36d4aaa9&q='
app=Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
#app.route('/weather',methods=['POST'])
def weather():
city=request.form['city_name']
url=api_address + city
json_data=requests.get(url).json()
temp_k=float(json_data['main']['temp'])
temp_c=temp_k-273.15
return render_template("weather.html",temp=temp_c)
if __name__=='__main__':
app.run(debug=True)
Here's the HTML code for 'home.html':
<!DOCTYPE html>
<html>
<body>
<p>
<form action='http://127.0.0.1:5000/' method="post">
City Name: <input type="text" name="city_name"> <br/>
<input type="submit" name="form" value="Submit">
</form>
</p>
</body>
</html>
HTML code for 'weather.html' is:
<!DOCTYPE html>
<html>
<body>
<h1>Temperature: {{temp}} degree celcius</h1>
<h1>Condition: {{desc}}</h1>
</body>
</html>
What should i do???
You are posting to http://127.0.0.1:5000/ instead of /weather
Try this:
<!DOCTYPE html>
<html>
<body>
<p>
<form action='/weather' method="post">
City Name: <input type="text" name="city_name"> <br/>
<input type="submit" name="form" value="Submit">
</form>
</p>
</body>
</html>
Related
I'm following an online course to learn about Flask.
I built a very simple set of HTML/Java/Flask.
On a POST or GET, I've tried both it's not passing the default value, world to the greet.html page when the input text is not populated with any text, just empty.
The specific line of code is:
return render_template("greet.html", name=request.form.get("name", "world"))
Through trial and error I've found the trigger in the following line.
<input id="name" autocomplete="off" autofocus placeholder="Enter Name" type="text" name="name">
If I remove the name="name" option from the input, the world is passed when nothing has been entered.
Here's the full test code:
app.py
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
return render_template("greet.html", name=request.form.get("name", "world"))
return render_template("index.html")
app.run(host='0.0.0.0', port=80)
layout.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello</title>
</head>
<body>
{% block body %}{% endblock %}
</body>
</html>
index.html
{% extends "layout.html" %}
{% block body %}
<form action="/" method="post">
<input id="name" autocomplete="off" autofocus placeholder="Enter Name" type="text" name="name">
<input type="submit">
</form>
{% endblock %}
greet.html
{% extends "layout.html" %}
{% block body %}
hello, {{ name }}
{% endblock %}
I expected this to just work.
As stated I already found the trigger, just not sure why.
Would like to see if anyone else has ran into this or if it's something I'm doing wrong or overlooked.
Using:
Flask 2.2.2
Python 3.9.0
Task in general:
The user should type in a text and the server should receive this text.
Problem
I get this error message when i run the code in VScode below.
What i am doing wrong?
My code files:
home.html
<!DOCTYPE html>
<html>
<body>
<h1>home</h1>
</body>
</html>
login.html
<!DOCTYPE html>
<html>
<body>
<h1>login</h1>
<form action="http://127.0.0.1:5500/login" method="POST">
<input type="text" placeholder="write your text here" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
local.py
from flask import Flask, request, render_template
app = Flask(__name__, template_folder='template')
#app.route('/')
def index():
#return 'tesddt'
return render_template('home.html')
#app.route('/login', methods=['GET', 'POST'])
def login():
print(request.form['name']) # should display 'website'
return render_template('login.html') #'Received !' + website # response to your request.
if __name__ == '__main__':
app.run(host="127.0.0.1", port=5500)
I use this Url: http://127.0.0.1:5500/login in my browser.
<!DOCTYPE html>
<html>
<body>
<h1>login</h1>
<form action="/login" method="POST">
<input type="text" placeholder="write your text here" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
Use the above code and check
i Have python server with flask running on it
from flask import Flask,render_template
app = Flask(__name__)
#app.route("/")
def main():
return render_template('index.html')
if __name__ == ("__main__"):
app.run(debug = True, host = "0.0.0.0", port =80)
this is index.html there is simple form and send button
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>whatl0ol</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
<div class="login">
<div class="heading">
<h2></h2>
<form action="#">
<div class="input-group input-group-lg">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" class="form-control" placeholder="">
</div>
<button type="submit" class="float">check</button>
</form>
</div>
</div>
</body>
</html>
i need solution to when user sends from index page, take it from python send to API.
need advices about site bandwith also.
thanks
make sure you have css files at this path
/your-project/static/css/style.css
also change your css in template to load it from static folder
<link href="{{ url_for('static', filename='css/style.css') }}" />
The "send..." button is not working, index page is running, drop-down menu ok, but when I click the "send..." nothing happen. (Result.html is ok, but empty of course) Any idea, what is the problem?
I've got these .py file:
from flask import Flask, request, render_template
from flask_wtf import Form
from flask_wtf import FlaskForm
app = Flask(__name__)
#app.route('/', methods=['POST', 'GET'])
def index():
return render_template('index.html')
#app.route('/result/', methods=['POST', 'GET'])
def result():
vehicle = request.form.get('wine')
year = request.form.get('year')
return render_template('result.html', vehicle=vehicle, year=year)
And two .html of course. index.html:
<!DOCTYPE html>
<html>
<body>
<h1>Submitting</h1>
<form action="/result" method="POST">
<label for="wine">wine</label>
<select id="wine" name="wine">
<option>red</option>
<option>white</option>
<option>rose</option>
</select>
<label for="year">Year</label>
<select id="year"name="year">
<option>1972</option>
<option>1999</option>
<option>2010</option>
</select>
</form>
<br>
<button type="submit" value="submit">send...</button>
</body>
</html>
The result.html:
<!DOCTYPE html>
<html>
<body>
{{ vehicle }} {{ year }}
</body>
</html>
replace your HTML with this HTML. you need to put submit button inside form tag to make it work.
<body>
<h1>Submitting</h1>
<form action="/result" method="POST">
<label for="wine">wine</label>
<select id="wine" name="wine">
<option>red</option>
<option>white</option>
<option>rose</option>
</select>
<label for="year">Year</label>
<select id="year"name="year">
<option>1972</option>
<option>1999</option>
<option>2010</option>
</select>
<button type="submit" value="submit">send...</button>
</form>
<br>
</body>
</html>
maybe you need to put your button in form tag ?
<form action="/result" method="POST">
...
<button type="submit" value="submit">send...</button>
</form>
I am trying to build a blog using flask. SimpleMDE is used as post editor(html code below). I want to save the markdown content to local file and render by flask-misaka in jinja2.
In SimpleMDE, I can get raw markdown content by simplemde.value(). But when I pass simplemde.value() to var in javascript. "\n" is missing aftering passing. I think it may have some "magic" tools in javascript. The html code return 2 alert message, first message contains line feed, the second doesn't.
Could somebody give me some hits about this problems?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Editor</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel='stylesheet' href='.{{ url_for("static", filename="css/simplemde.min.css") }}'>
<script src='.{{ url_for("static", filename="js/simplemde.min.js") }}'></script>
</head>
<script type='text/javascript'>
function check() {
var raw = simplemde.value();
alert(raw);
document.testform.markdown_raw.value=raw;
alert(document.testform.markdown_raw.value);
}
</script>
<body>
<form method='post' class='form' role='form' name='testform'>
<div class="form-group " style="padding-top:10px">
<input class="form-control" id="post_title" name="post_title" type="text" value="Title?">
</div>
<div class="form-group">
<input class="form-control" id="markdown" name="post_content" type="textarea" value="">
</div>
<div class="form-group" style='display:none'>
<input class="form-control" id="markdown_raw" name="markdown_raw" type="textarea" value="Origin">
</div>
<div>
<input class='btn btn-default' onclick="check();" id='submit' name='submit' type='submit' value='Save'>
<input class='btn btn-default' id='publish' name='publish' type='submit' value='Publish'>
</div>
</form>
<script type='text/javascript'>
var simplemde = new SimpleMDE({ element: document.getElementById('markdown') });
</script>
</body>
</html>
If you want to get raw markdown, just use simplemde.value().
When you put raw markdown into normal textarea, it turns into pure text.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Editor</title>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.css">
<script src="https://cdn.jsdelivr.net/simplemde/latest/simplemde.min.js"></script>
</head>
<body>
<form method='post' class='form' role='form' name='testform'>
<div class="form-group " style="padding-top:10px">
<input class="form-control" id="post_title" name="post_title" type="text" value="Title?">
</div>
<div class="form-group">
<textarea class="form-control" id="markdown" name="post_content"></textarea>
</div>
<div>
<input class='btn btn-default' onclick="" id='submit' name='submit' type='submit' value='Save'>
<input class='btn btn-default' id='publish' name='publish' type='submit' value='Publish'>
</div>
</form>
<script type='text/javascript'>
var simplemde = new SimpleMDE({ element: document.getElementById('markdown') });
</script>
</body>
</html>
Get the content:
from flask import Flask, render_template, request
app = Flask(__name__)
#app.route('/', methods=['GET', 'POST'])
def test():
if request.method == 'POST':
raw_md = request.form['post_content']
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)