Search code examples
pythonwsgiwerkzeug

Help with Python in the web


I've been using Werkzeug to make WSGI compliant applications. I'm trying to modify the code in the front page.

Its basic idea is that you go to the /hello URL and you get a "Hello World!" message. You go to /hello/ and you get "hello !". For example, /hello/jeff yields "Hello Jeff!". Anyway, what I'm trying to do is putting a form in the front page with a text box where you can enter your name, and it will submit it to /hello. So if you enter "Jeff" in the form and submit, you get the "Hello Jeff!" message.

However, I have no idea how to do this. I need to pass the "name" variable to the hello template, but I don't know how. Here's my index.html:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Index page</title>
</head>

<body>
<h1>Go to the <a href="${url_for('say_hello')}">default</a></h1>
<form name="helloform" action="${url_for('say_hello')}" method="post">
<input type="text" name="name">
<input type="submit">
</form>
</body>
</html>

method="get" doesn't work either, predictably.


Solution

  • Do it the right way: go to /hello?name=joe to say hello to joe, and so forth. That's how HTML/HTTP is designed to work! Your code behind the /hello URL just needs to get the name parameter from the request, if present, and respond accordingly.