URL

□未翻訳

□翻訳中

□翻訳完了(細田謙二)

■レビュー(中垣健志)

URL

URL関数は、web2pyにおいて最も重要な関数の1つです。これは、アクションと静的ファイルのための内部URLのパスを生成します。

The URL function is one of the most important functions in web2py. It generates internal URL paths for the actions and the static files.

たとえば、これは:

Here is an example:

1.

URL('f')

次のようにマッピングされます

is mapped into

1.

/[application]/[controller]/f

ただし、URL関数の出力は現在のアプリケーションの名前や、呼んだコントローラ、他のパラメータに依存します。URLのマッピングや、逆向きのURLのマッピングもサポートしています。URLのマッピングによって、外部URLの形式を再定義することができます。URL関数をすべての内部URLの生成に用いる場合、URLマッピングに追記や変更を加えることで、web2pyアプリケーション内のリンク切れが防止されます。

Notice that the output of the URL function depends on the name of the current application, the calling controller and other parameters. web2py supports URL mapping and reverse URL mapping. URL mapping allows to redefine the format of external URLs. If you use the URLfunction to generate all the internal URLs, then additions or changes to URL mappings will prevent broken links within the web2py application.

URL関数に追加のパラメータ、すなわち、URLのさらなるパス(args)やURLのクエリ変数(vars)を渡すことができます:

You can pass additional parameters to the URL function, i.e., extra terms in the URL path (args) and URL query variables (vars):

1.

URL('f', args=['x', 'y'], vars=dict(z='t'))

これは、次のようにマッピングされます

is mapped into

1.

/[application]/[controller]/f/x/y?z=t

args属性は、web2pyによって自動的に解析され、復号化され、最後に、request.argsに格納されます。同様に、 varsは、解析され、復号化され、そして、request.varsに格納されます。argsとvarsは、web2pyがクライアントのブラウザと情報の交換をするための基本的な仕組みを提供します。

The args attributes are automatically parsed, decoded, and finally stored in request.args by web2py. Similarly, the vars are parsed, decoded, and then stored in request.vars. args andvars provide the basic mechanism by which web2py exchanges information with the client's browser.

argsが1つの要素しか含まない場合、リストにしてそれを渡す必要はありません。

If args contains only one element, there is no need to pass it in a list.

URL関数は、他のコントローラや他のアプリケーションのURLを生成するためにも用いることができます:

You can also use the URL function to generate URLs to actions in other controllers and other applications:

1.

URL('a', 'c', 'f', args=['x', 'y'], vars=dict(z='t'))

これは、次のようにマッピングされます

is mapped into

/a/c/f/x/y?z=t

アプリケーション、コントローラ、関数を名前付き引数で指定することも可能です:

It is also possible to specify application, controller and function using named arguments:

1.

URL(a='a', c='c', f='f')

アプリケーションの名前aがない場合、現在のアプリが想定されます。

If the application name a is missing the current app is assumed.

1.

URL('c', 'f')

コントローラの名前がない場合は、現在のものが想定されます。

If the controller name is missing, the current one is assumed.

1.

URL('f')

コントローラの関数の名前を渡す代わりに、関数自身を渡すことも可能です。

Instead of passing the name of a controller function it is also possible to pass the function itself

1.

URL(f)

上で説明した理由により、アプリケーションで静的ファイルのURLを生成するには、URL関数を常に用いるべきです。静的ファイルはアプリケーションのstaticサブフォルダに保存されています(管理インターフェースを用いてアップロードすることができる場所です)。web2pyは仮想的な'static'コントローラを提供し、それによってstaticサブフォルダからファイルが取り出され、そのcontent-typeが決められ、クライアントにファイルがストリームされます。次の例では、"image.png"という静的ファイルに対するURLを生成しています:

For the reasons mentioned above, you should always use the URL function to generate URLs of static files for your applications. Static files are stored in the application's static subfolder (that's where they go when uploaded using the administrative interface). web2py provides a virtual 'static' controller whose job is to retrieve files from the static subfolder, determine their content-type, and stream the file to the client. The following example generates the URL for the static file "image.png":

1.

URL('static', 'image.png')

これは、次のようにマッピングされます

is mapped into

1.

/[application]/static/image.png

argsとvarsの引数はエンコード/エスケープする必要はありません。それは自動的に行われるからです。

You do not need to encode/escape the args and vars arguments; this is done automatically for you.

デフォルトでは、現在のリクエストに対応する拡張子(request.extension)は、request.extensionがデフォルトのhtmlでない限り、関数に追加されます。これを書き換えるには、URL(f='name.ext')のように関数名の一部として拡張子を明示的に含めるか、次のように拡張子の引数によって行います:

By default, the extension corresponding to the current request (request.extension) is appended to the function, unless request.extension is html, the default. This can be overridden by explicitly including an extension as part of the function nameURL(f='name.ext') or with the extension argument:

1.

URL(..., extension='css')

現在の拡張子を明示的に抑制するには次のようにします:

The current extension can be explicitly suppressed:

1.

URL(..., extension=False)