2

I am studying Python recently and a beginner. I am unable set a logic to as, if the post data does not qualify, post page will be redirected to origin page.

After some research I found this how to redirect one page to another, but still having confusion regarding redirecting page.

Here is my html code :

<form action="generate_timing_data.py" method="POST"> <p>Select an athlete from the list to work with: </p><input type="radio" name="which_athlete" value="Mikey McManus"> Mikey McManus<br /> <input type="radio" name="which_athlete" value="Julie Jones"> Julie Jones<br /> <input type="radio" name="which_athlete" value="James Lee"> James Lee<br /><input type="radio" name="which_athlete" value="Sarah Sweeney"> Sarah Sweeney<br /><p> </p><input type=submit value="Select"> </form> And this is my python code :

import cgi
import athletemodel
import yate
form_data = cgi.FieldStorage()
if form_data['which_athlete'].value != '':
   //calculation stuff
else :
   //redirect stuff

In here, where to put header code and all the staff in form and how to redirect to origin page from the post page, how to set all the things? please help me to understand how the process flow?

halfer
  • 19,824
  • 17
  • 99
  • 186
Prad
  • 150
  • 1
  • 4
  • 15
  • 1
    It's hard not to downvote when you've given such limited details. Why have you only shown the html? All the stuff you mention happens in the Python code. – Daniel Roseman Aug 30 '17 at 11:16
  • sure wait ,I am giving you py codes – Prad Aug 30 '17 at 11:23
  • 1
    wrt to your "how to redirect one page to another" link: it has very few to do with your problem (the question you link to is about HTTP client code, not about HTTP server code which is what you're doing). Also if you hope to do any web programming, you need to understand the HTTP protocol (both the request and response parts). – bruno desthuilliers Aug 30 '17 at 11:30
  • @burno desthuilliers ,Is this really required to understand HTTP protocol for sending form post data to a CGI py page ? – Prad Aug 30 '17 at 11:41
  • Please anyone ,need little help . – Prad Aug 31 '17 at 10:31

1 Answers1

3
#! /usr/bin/python3
import cgi
form_data=cgi.FieldStorage()
print('Content-type:text/html\n\n')
if 'username' not in form_data or 'password' not in form_data:
   redirectURL = "http://localhost:8081/"
   print('<html>')
   print('  <head>')
   print('    <meta http-equiv="refresh" content="0;url='+str(redirectURL)+'" />') 
   print('  </head>')
   print('</html>')
else:
   print(form_data['username'].value)
   print(form_data['password'].value)

I did lot of R&D and finally found this and would be the perfect solution so far.If you feel not appropriate ,please correct it.Thank you

Prad
  • 150
  • 1
  • 4
  • 15