HTTPとredirect

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

HTTP redirect

web2pyはHTTPという新しい例外を1つだけ定義しています。この例外は、モデル、コントローラ、ビューのどこにおいても、次のコマンドで発生させることができます:

web2py defines only one new exception called HTTP. This exception can be raised anywhere in a model, a controller, or a view with the command:

1.

raise HTTP(400, "my message")

これによって、フロー制御はユーザのコードからジャンプしてweb2pyに戻り、次のようなHTTPレスポンスを返します:

It causes the control flow to jump away from the user's code, back to web2py, and return an HTTP response like:

1.

2.

3.

4.

5.

6.

7.

8.

9.

HTTP/1.1 400 BAD REQUEST

Date: Sat, 05 Jul 2008 19:36:22 GMT

Server: Rocket WSGI Server

Content-Type: text/html

Via: 1.1 127.0.0.1:8000

Connection: close

Transfer-Encoding: chunked

my message

HTTPの最初の引数はHTTPステータスコードです。2番目の引数はレスポンスのボディとして返される文字列です。その他のオプション的な名前付き引数は、HTTPレスポンスヘッダを作成するために使用されます。たとえば:

The first argument of HTTP is the HTTP status code. The second argument is the string that will be returned as the body of the response. Additional optional named arguments are used to build the response HTTP header. For example:

1.

raise HTTP(400, 'my message', test='hello')

これは以下を生成します:

generates:

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

HTTP/1.1 400 BAD REQUEST

Date: Sat, 05 Jul 2008 19:36:22 GMT

Server: Rocket WSGI Server

Content-Type: text/html

Via: 1.1 127.0.0.1:8000

Connection: close

Transfer-Encoding: chunked

test: hello

my message

もし開いているデータベーストランザクションをコミットしたくなければ、例外が発生する前にロールバックしてください。

If you do not want to commit the open database transaction, rollback before raising the exception.

HTTP以外のいかなる例外では、web2pyによって、すべての開いているデータベーストランザクションがロールバックされ、エラーのトレースバックがログに保存され、訪問者に対するチケットが発行され、標準的なエラーページが返されます。

Any exception other than HTTP causes web2py to roll back any open database transaction, log the error traceback, issue a ticket to the visitor, and return a standard error page.

これは、HTTPだけがページ間のフロー制御のため使用することができることを意味します。その他の例外は、アプリケーションによって捕捉されなければならず、そうしないとweb2pyによってチケットが切られます。

This means that only HTTP can be used for cross-page control flow. Other exceptions must be caught by the application, otherwise they are ticketed by web2py.

次のコマンドは:

The command:

1.

redirect('http://www.web2py.com')

単に以下のショートカットです:

is simply a shortcut for:

1.

2.

3.

raise HTTP(303,

'You are being redirected <a href="%s">here</a>' % location,

Location='http://www.web2py.com')

このHTTPを初期化するメソッドの名前付き引数は、HTTPヘッダのディレクティブへと変換されます。この場合はリダイレクト先の場所になります。redirectは、リダイレクトのためのHTTPステータスコード(デフォルトでは303)をオプション的な2番目の引数にて受け取ります。この数字を307に変更すると一時的なリダイレクトになり、301に変更すると永久的なリダイレクトになります。

The named arguments of the HTTP initializer method are translated into HTTP header directives, in this case, the redirection target location. redirect takes an optional second argument, which is the HTTP status code for the redirection (303 by default). Change this number to 307 for a temporary redirect or to 301 for a permanent redirect.

ユーザリダイレクトの最も一般的な方法は、次のように同じアプリ内の他のページにリダイレクトし、(オプション的に)パラメータを渡すことです:

The most common way to user redirect is to redirect to other pages in the same app and (optionally) pass parameters:

1.

redirect(URL('index',args=(1,2,3),vars=dict(a='b')))