?
Solved

Redirect to another html page with python cgi

Posted on 2014-01-06
5
Medium Priority
?
2,789 Views
Last Modified: 2014-01-20
I have a python cgi script that processes form values on mydomain.com:

if 'Submit1' in form:
        url = 'http://www.domain.com/page1.html'
        print urllib2.urlopen(url).read()
                           
elif 'Submit2' in form:
        url = 'http://www.domain.com/page2.html'
        print urllib2.urlopen(url).read() 

Open in new window


When the user selects a Submit button, the destination page is loaded.  But the url continues to show the script (http://www.domain.com/myscript.py) instead of the page url.  Bookmarking the script url will fail and it doesn't look right: depending on how the page is accessed by the user, two different urls will appear in the browser location bar for the same resource.  

I'm testing this on localhost running apache and nothing works so far. Help.
0
Comment
Question by:sara_bellum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 39758701
The methods you are using just read the content of the URL and do not redirect.  I think this http://docs.python.org/2/library/urllib2.html#httpredirecthandler-objects is what you need.
0
 

Accepted Solution

by:
sara_bellum earned 0 total points
ID: 39760789
I know. Thanks for the link, but I had tried a number of the solutions suggested on the page and the result was always a blank page with no change to the url.  

But thankfully there's a way around the problem that I share in case it's useful to someone. My script now contains:

if hd and x and y and z:
             
        title = ('%s, %s %s %s' % (hd, x, y, z))

    elif rd and x and y and z:
        
         title = ('%s, %s %s %s' % (rd, x, y, z))
        
    build_url = os.path.join(web_base, file_name + '.html')
    url = os.path.join('http://localhost', build_url)    
            
    try:
        t = Template(open(template_form).read())
        print(t.substitute(title=title, x=x, y=y, url=url))    

Open in new window


The html page now contains a form for each submit button pointing to their respective urls. The links are not entirely static, but this works because the script's template substitution passes on the values as arguments:

<form action="${url}" method="get">
    <input type="hidden" name="x" value="${x}">
    <input type="hidden" name="y" value="${y}">  
    <input type="Submit" name="Submit1" value="Return to ${y}">&nbsp;
    </form>
   
    <form action="/path-to-page2.html" method="get">
    <input type="submit" value="Home" name="Submit2" />
    </form>

Open in new window

0
 

Author Comment

by:sara_bellum
ID: 39760792
I added cgi.escape to each string substitution in the title but I'm not sure that this provides any protection because the arguments are listed in the location bar when the user clicks on a submit button.  Let me know if I should use that feature in this case thanks.
0
 

Author Comment

by:sara_bellum
ID: 39784394
cgi.escape should be used whenever user input is involved.
0
 

Author Closing Comment

by:sara_bellum
ID: 39793773
Because my solution worked.
0

Featured Post

Create the perfect environment for any meeting

You might have a modern environment with all sorts of high-tech equipment, but what makes it worthwhile is how you seamlessly bring together the presentation with audio, video and lighting. The ATEN Control System provides integrated control and system automation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses
Course of the Month8 days, 22 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question