Solved

Redirect to another html page with python cgi

Posted on 2014-01-06
5
2,193 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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

706 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now