Solved

Redirect to another html page with python cgi

Posted on 2014-01-06
5
2,421 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
  • 4
5 Comments
 
LVL 83

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

828 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