1

I am learning web development in Python. When I open the HTML I get the Ferrari Fiat Ford, which is what I am expecting but then I click on Ferrari and it opens up the new page as make, model, which is not what I want. I want Ferrari Dino.

Could you help me understand what is the problem?

<!DOCTYPE HTML>
<html lang ="en">
<head>
    <meta charset="UTF-8">
    <title>Python Response</title>
</head>
<body>
    <h1>
       <a href="get.py?make=Ferrarri&model=Dino">Ferrari</a>
       <a href = 'get.py?make = Fiat & model = Topolino'>Fiat</a>
       <a href = 'get.py?make = Ford & model = Mustang'>Ford</a>
    </h1>
</body>
</html>

Python

import cgi

data = cgi.FieldStorage()

make = data.getvalue('make')
model = data.getvalue('model')

print ( 'Content-type:text/html\r\n\r\n' )
print ( '''<!DOCTYPE HTML><html lang = "en">
        <head>
            <meta charset="UTF-8">
            <title>Python Response</title>
        </head>
        <body>
            <h1>, make, model,</h1>
            <a href = "get.html">Back</a>
        </body>
        </html>''' )
2
  • I suggest a better question title, one that describes the problem in general, for example "Issue with HTTP GET Parameters in Python CGI" Commented Oct 14, 2014 at 1:32
  • If you wanna learn about how should a Python web application look like, ignore everything you are trying to do and follow this guide blindly: flask.pocoo.org/docs/0.10/quickstart. Commented Oct 14, 2014 at 1:39

3 Answers 3

2

A variable cannot be a string.

import cgi

data = cgi.FieldStorage()

make = data.getvalue('make')
model = data.getvalue('model')

print ( 'Content-type:text/html\r\n\r\n' )
print ( '''<!DOCTYPE HTML><html lang = "en">
        <head>
            <meta charset="UTF-8">
            <title>Python Response</title>
        </head>
        <body>
            <h1>, ''' + make + ', ' + model + ''',</h1>
            <a href = "get.html">Back</a>
        </body>
        </html>''' )
Sign up to request clarification or add additional context in comments.

Comments

1

After testing this out, the HTML is responding as anticipated.

My recommendation goes along lines of this question here: How to pass python variable to html variable?

In summary, you could do this in a better way by writing a function and returning the html. Also, once you've written the python function, rather than have the:

<h1>, make, model,</h1>

You could use a substitution in your python function like so:

"<h1>, %s, %s,</h1>" % (make, model)

Which would be located in the same python file as the two of these variables.

Comments

0

Your template outputs this:

<h1>, make, model,</h1>

And that's all it ever will output. You can instead change it a little and then use the format method to insert values into it.

html = '''<!DOCTYPE HTML><html lang = "en">
    <head>
        <meta charset="UTF-8">
        <title>Python Response</title>
    </head>
    <body>
        <h1>, {make}, {model},</h1>
        <a href = "get.html">Back</a>
    </body>
    </html>'''

print html.format(make=make, model=model)

The values in curlybraces, {make} and {model}, are named tokens inside the string. When you use format() on the string called html, you replace those tokens. In this case, I referenced them directly by name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.