Python Bottle how to pass parameter as json - python

I created an api for openerp using bottle
It works well while access using browser
I don't know how to pass it as json parameters
The Problem is
how can i call using api and pass json parameters like
http://localhost/api?name=admin&password=admin&submit=Submit
Here is my wsgi code app.wsgi
import json
import os
import sys
import bottle
from bottle import get, post, run,request,error,route,template,validate,debug
def login():
import xmlrpclib
username = request.forms.get('name')
pwd = request.forms.get('password')
dbname = 'more'
sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
uid = sock_common.login(dbname, username, pwd)
if uid:
return json.dumps({'Success' : 'Login Sucessful'])
def index():
return '''
<html>
<head>
<title> Portal</title>
</head>
<body>Welcome To PORTAL
<form method="GET" action="/api/links" enctype="multipart/form-data">
Name:<input name="name" type="text"/><br>
Password:<input name="password" type="password"/><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>'''
def links():
return '''
<html>
<head>
<title> Portal</title>
</head>
<body>
<a href="/api/advisor">Advisor<br>
</body>
</html>'''
application = bottle.default_app()
application.route('/', method="GET", callback=index)
application.route('/', method="POST",callback=login)

request.forms is used for POST or PUT requests. The form in your code uses GET, not POST, so you should use request.query.getall, which gives you access to "URL arguments".

I don't see anything wrong with the code (except pep8 changes), only problem I see is method of the form and location, see the fixed version below ...
import json
import os
import sys
import bottle
from bottle import get, post, run, validate, request, error, route, template, debug
def login():
import xmlrpclib
username = request.forms.get('name')
pwd = request.forms.get('password')
dbname = 'more'
sock_common = xmlrpclib.ServerProxy ('http://localhost:8069/xmlrpc/common')
uid = sock_common.login(dbname, username, pwd)
if uid:
return json.dumps({'Success': 'Login Sucessful'})
def index():
return '''
<html>
<head>
<title> Portal</title>
</head>
<body>Welcome To PORTAL
<form method="POST" action="/" enctype="multipart/form-data">
Name:<input name="name" type="text"/><br>
Password:<input name="password" type="password"/><br>
<input type="submit" value="Submit" name="submit">
</form>
</body>
</html>'''
def links():
return '''
<html>
<head>
<title> Portal</title>
</head>
<body>
<a href="/api/advisor">Advisor<br>
</body>
</html>'''
application = bottle.default_app()
application.route('/', method="GET", callback=index)
application.route('/', method="POST", callback=login)
application.run()

Related

Browser back button takes user back inside after Logout

I have been working on a web interface using Flask and having some issues with back button in browser as after logging out hitting it takes user back inside. I have found similar questions and tried their answers but the issue is not resolved. I am attaching a simple example kindly have a look at it.
Main
from flask import Flask, request,session, redirect, url_for, render_template
from os import urandom
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
app.secret_key = urandom(24)
#app.route('/', methods=['POST', 'GET'])
def index():
if request.method == 'POST':
session['Email id'] = request.form.get('Email Id')
Pass = request.form.get('Password')
try:
if session['Email id'] == 'KK#gmail.com' and Pass == 'KKK':
return render_template('Logged_in.html')
except:
return render_template('login.html')
return render_template('login.html')
#app.route('/sign_out')
def sign_out():
session.pop('Email id')
return redirect(url_for('index'))
#app.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
r.headers['Cache-Control'] = 'public, max-age=0'
return r
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True, threaded=True)
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="{{ url_for('index') }}" method="POST" id="login" class="input-group">
<input type="text" class="input-field" placeholder="Email Id" required name="Email Id">
<input type="text" class="input-field" placeholder="Password" required name="Password">
<button type="submit" class="submit-btn" style="color: white;">Log in</button>
</form>
</body>
</html>
Logged_in.html
<h2>You are Logged in</h2>
<i class="fas fa-sign-out-alt"></i>Log out
Your problem is that when users push the back button their browser will re-do the POST request. You need to use the POST/redirect/GET pattern to prevent this. For this you need four endpoints in totalt:
GET / : Check in the session that the user is logged in and render Logged_in.html, otherwise redirect to /login.html
GET /login.html : Render login.html
POST /sign_in : Check username and password. If successful, update the session and redirect to /
POST /sign_out : Log out the user session and redirect to /login.html
Do not render templates in your POST endpoints, just make them manipulate the session and then redirect.

Form request flask error when trying to submit

i am a beginner programmer in flask and i encounter a problem i rly don't see the problem with my code.
In the login file i have a form and it will send a req to /check but i get an err like: The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
I also have the register file with the action route '/' and this works.
Here is the code:
from flask import Flask, render_template, request, url_for, redirect
from models.Data_Base import DataBase
app = Flask(__name__)
db = DataBase()
#app.route('/')
def main_page_render():
return render_template('home.html')
#app.route('/login')
def login_page_render():
return render_template('login.html')
#app.route('/check')
def check_page_render():
print('{} {}'.format(request.form['user_name'], request.form['password']))
if request.form['user_name'] == 'admin' and request.form['password']:
return redirect(url_for('admin_page_render'))
elif db.verify_login(request.form['user_name'], request.form['password']) == True:
return redirect('/fighter/{}'.format(request.form['user_name']))
#app.route('/admin')
def admin_page_render():
return 'Admin Page'
#app.route('/fighter/<username>')
def fighter_page_render(username):
return 'Fighter page'
#app.route('/register')
def register_page_render():
return render_template('register.html')
if __name__ == '__main__':
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login_page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="/check">
<label for="user_name">User name:</label><br>
<input type="text" id="user_name" name="user_name"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
<p>Click submit in order to get to your account</p>
</body>
</html>
A Solution can be this, try to do these changes.
app.py
...
#app.route('/check', methods=["GET", "POST"])
def check_page_render():
print('{} {}'.format(request.form['user_name'], request.form['password']))
if request.form['user_name'] == 'admin' and request.form['password']:
return redirect(url_for('admin_page_render'))
elif db.verify_login(request.form['user_name'], request.form['password']) == True:
return redirect('/fighter/{}'.format(request.form['user_name']))
...
/templates/home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>login_page</title>
</head>
<body>
<h2>Login Page</h2>
<form action="/check" method="POST">
<label for="user_name">User name:</label><br>
<input type="text" id="user_name" name="user_name"><br>
<label for="password">Password:</label><br>
<input type="password" id="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
<p>Click submit in order to get to your account</p>
</body>
</html>

Google Search using python

I get data from input. When I click to search button I need to open webbrowser page and see my input data in google. How can I do that? Many thanks.
This is my code:
views.py:
from django.http import HttpResponse
from django.shortcuts import render
def index(request):
if request.method == 'GET':
return render(request, 'index.html', context={})
# Handles the search once the submit button in the form is pressed
# which sends a "POST" request
if request.method == 'POST':
# Get the input data from the POST request
search_query = request.POST.get('search', None)
# Validate input data
if search_query and search_query != "":
return HttpResponse(search_query)
try:
from googlesearch import search
except ImportError:
print("No module named 'google' found")
for j in search(search_query, tld="co.in", num=10, stop=1, pause=2):
print(j)
else:
return HttpResponse('Invalid input.')
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form method="POST">
{% csrf_token %}
<input type="text" name="search" placeholder="some text"><br>
<button class="button" name="submit" type="submit">Search</button>
</form>
</body>
</html>
urls.py
from django.urls import path
from firstapp import views
urlpatterns = [
path('', views.index, name='home')
]
All files are in hello folder. My app namely firstapp path: C:\Users\user\Desktop\hello\firstapp
index.html path is:
C:\Users\user\Desktop\hello\firstapp\templates
In order to redirect to google you need to have your search textfield to use a parameter called q. Here is an index.html file that would work:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
</head>
<body>
<form action="https://google.com" method="GET">
{% csrf_token %}
<input type="text" name="q" placeholder="some text"><br>
<input type="submit" name="submit" value="Search">
</form>
</body>
</html>

Python global variable not updating in real time flask

value=user_id is not taking the new value defined by def login():.for e.g. if the entered user_id from HTML page is 200, 300 and 100. The values will be assigned as 0, 200, 300. That is, when home page is displayed it will be showing user 0 for entered value 200, and for second time it is showing 200 for entered value 300 and for third time it is showing 300 for entered value 100.
I need a real time values i.e. for entered value 200 home page should have value 200, and for 300 it should be 300. Could anyone please help me with this? Please advice. Thanks in advance. Note: HTML code is not optimized.
from flask import Flask, render_template # Flask is the class within the flask library
from flask import request, redirect
user_id = int(0)
app = Flask(__name__) # Instance of the object Flask. __name__: this gets value of name of python script
#app.route('/', methods=["GET", "POST"]) # login page
def login():
if request.method == "POST":
global user_id
user_id = request.form['userid']
print(user_id)
return render_template("login.html")
#app.route('/home/') # This the URL i.e. home page
def home():
return render_template("home.html", value=user_id)
if __name__ == "__main__":
app.run(debug=True)
HTML Code for Login page here.
<!DOCTYPE html>
<html>
<head>
<title>Recommender System App</title>
<link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/main.css')}}">
</head>
<body>
<header>
<div class="container">
<h1 class="logo">Movie Recommender System</h1>
<strong><nav>
<ul class="menu">
</ul>
</nav></strong>
</div>
</header>
<form action="{{ url_for('login') }}" method="post">
<div class = "Login Fields">
<b>Username:</b> <input type="number" placeholder="Numbers only" name='userid'>
<p><b>Password:</b> <input type="password" placeholder="Enter Password" name= 'password' id="pwd"></p>
<input type="submit" onclick="check(this.form)" class="button">
</div>
<p></p>
<p></p>
<div>
<p>Test User IDs:</p>
<li>224</li>
<li>216</li>
<li>681</li>
<li>19</li>
<li>82</li>
<li>305</li>
<li>44</li>
<li>268</li>
<p>Password: 123Swaroop</p>
</div>
</form>
<script language="javascript">
function check(form)/*function to check userid & password*/
{
if(form.password.value == "123Swaroop")
{
window.open(href = "{{ url_for('home') }}")
}
else
{
alert("Wrong Password or User Id")/*displays error message*/
}
}
</script>
</body>
</html>
HTML code for home page here:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link rel="stylesheet" type="text/css" href="{{url_for('static',filename='css/main.css')}}">
</head>
<body>
<header>
<div class="container">
<h1 class="logo">Welcome User: {{ value }}</h1>
<strong><nav>
<ul class="menu">
</ul>
</nav></strong>
</div>
</header>
<div class = "home">
<h1>Top Rated Movies</h1>
<p>This is test website</p>
</div>
</body>
</html>
You also need to call the global value within your home() function:
from flask import Flask, render_template, request, redirect
user_id = int(0)
app = Flask(__name__)
#app.route('/', methods=["GET", "POST"])
def login():
if request.method == "POST":
global user_id
user_id = request.form['userid']
print(user_id)
return render_template("login.html")
#app.route('/home/')
def home():
global user_id
return render_template("home.html", value=user_id)
if __name__ == "__main__":
app.run(debug=True)
However, using global variables is usually considered bad practice so you could also consider something like this:
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
#app.route('/', methods=["GET", "POST"])
def login():
return render_template("login.html")
def get_user_id():
try:
if request.method == "POST":
user_id = request.form['userid']
return user_id
else:
# Handle error or do something else
except:
# Handle error or do something else
#app.route('/home/')
def home():
try:
return render_template("home.html", value=get_user_id())
except:
# Handle error or do something else
if __name__ == "__main__":
app.run(debug=True)

Passing data with GET request instead of POST in flask app

I am building flask app and I am trying to understand the routes , methods in flask documentation. I wrote a code which uses GET to submit the fields of a form :
#app.route('/',endpoint='buf')
def index():
page = """
<DOCTYPE! html>
<html lang="en-US">
<head>
<meta charset=utf-8">
</head>
<body>
<form action="/hello" method="GET">
First name: <input type="text" name="fname" id="fname" ><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
"""
return page
#app.route('/hello',endpoint="new",methods=['GET','POST'])
def index():
if request.method=='POST':
return 'Hello %s' % (request.form['fname'])
else:
return 'Hello %s' % (request.form['fname'])
I get an error when I use 'GET' instead of 'POST' in my html form tag. Is there a way I can access the fields of the form using GET instead of POST?
From the relevant section at the quickstart guide
To access parameters submitted in the URL (?key=value) you can use the args attribute:
searchword = request.args.get('key', '')

Categories

Resources