Reputation: 1920
I am calling my python script after a HTML form is submitted by the user . My python script is present in usr/lib/cgi-bin . I want to redirect to another page depending on the inputs provided by the user , basically I am creating a login script where if the user inputs are valid then the user will be redirected to the loggedin.html page else to error.html page
signin.py (i am trying to execute this code)
#! /usr/bin/python2.7
import cgi, cgitb
# import pymongo module for connecting to mongodb database
import pymongo
from pymongo import MongoClient
# Create instance of FieldStorage
form = cgi.FieldStorage()
# creating a mongo client to the running mongod instance
client = MongoClient()
# selecting the database mydb
db_mydb = client.mydb
# selecting the collection user
collection_user = db_mydb.user
print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Signin</title>"
print "</head>"
# Get data from fields
email = form.getvalue('login-username')
password = form.getvalue('login-password')
#checking whether user inputs are correct or not
existence_query=collection_user.find_one({"_id":email,"password":password})
print "<body>"
if(existence_query):
print "person exists"
print "Status: 301 Moved\r\n\r\n"
print "Location : http://localhost/mongo/loggedin.html\r\n\r\n"
else:
print "person does't exists"
#redirecting to error.html page
print "</body>"
print "</html>"
Output that I am getting after this script is called from my html form (when user inputs are valid i.e user is a valid):
person exists Location:http:localhost/mongo/loggedin.html
I am new to cgi using python so any help would be great to me
Upvotes: 1
Views: 2458
Reputation: 1920
# Import modules for CGI handling
import cgi, cgitb
# import pymongo module for connecting to mongodb database
import pymongo
from pymongo import MongoClient
# Create instance of FieldStorage
form = cgi.FieldStorage()
# creating a mongo client to the running mongod instance
# The code will connect on the default host and port i.e 'localhost' and '27017'
client = MongoClient()
# selecting the database mydb
db_mydb = client.mydb
# selecting the collection user
collection_user = db_mydb.user
#print "Content-type:text/html\r\n\r\n"
# Get data from fields
email = form.getvalue('login-username')
password = form.getvalue('login-password')
#checking whether user inputs are correct or not
existence_query = collection_user.find_one({"_id":email,"password":password})
if(existence_query):
print "Location:http://localhost/mongo/index.html\r\n"
print "Content-type:text/html\r\n\r\n"
else:
print "Location:http://localhost/mongo/index.html\r\n"
print "Content-type:text/html\r\n\r\n"
Headers need to be initialized in the starting before any other data being sent to the browser.In the question print statements like print "<html>
"were written before location header and so the desired result wasn't achieved.
Upvotes: 2
Reputation: 44838
From Python docs on the cgi
module
The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is following.
You have to send headers before
any other data you want to send. To accomplish your goal, just put all the header sending logic before the HTML-sending one.
But you'd better move all the form-checking code to another file so it doesn't print anything, just sends redirects and stuff.
Upvotes: 1