views:

199

answers:

5

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"&gt;
<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.

A: 

I get the feeling that what you are trying to do is too basic. Most webframeworks, and Werkzeug is no different, are about feeding data into a database, and then displaying that data. You seem to try to skip the database part.

This is surely possible, but I guess tutorials don't tell you how.

Basically you need to in the python code that handles the page, find the request object. This will have the form data, including the "name". You need to feed that to the template in the template rendering.

Lennart Regebro
A: 

Directly on the page link to which you provide (http://werkzeug.pocoo.org/) when clicking on 'Click here', you get a code for the hello X example. What you seem to be missing is:

Hello ${url_values['name']|h}!

somewhere in your html template (assuming it is the template for response as well as for request)

van
A: 

HTML Forms have a static target address, action="/something", but you want to change the address depending user input. You have two options:

  1. Add javascript to the html form to change the target address (by appending the name) before the form is submitted.

  2. Write a new method in your web framework that reads GET or POST variables and point the form there.

THC4k
Adding javascript to do this is a really bad idea. It would mean that your site links (about as basic as HTML gets) were broken on any browser without javascript (which includes a lot of disabled users etc., and could be considered (illegal) discrimination.
Lee B
A: 

HTML is based on the REST principle... each url should be an object, like a person; NOT an action, like saying hello to a person. Have the URL identify who you want to greet only if your web application knows who that is internally, by looking at its database.

If your webapplication has no object called Joe, then designing URLs with Joe in them is not the right approach. Your particular web application is about finding out who people are and sending a hello messages to them. So, you should probably have one URL: /greeter, which looks for information sent in GET or POST requests (from your form), and displays a greeting. If it doesn't know who to greet, it can display the form to find out.

Always think in terms of the objects you're actually working with --- the components that make up the system --- when building software.

Lee B
+1  A: 

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.

Alex Martelli