URL Template

A URL Template is a way to specify a URL that includes parameters that must be substituted before the URL is resolved. The syntax is usually to enclose the parameter in Braces ({example}). The convention is for a parameter to not be Percent encoded unless it follows a Question Mark (?).

Examples

If we were building these urls for Björk with firstName=Björk and lastName=Guðmundsdóttir they would be:

Code

In Python 2.x, if you have the variable name that you want to put into the string url, you can simply do this:

import urllib2

while "{name}" in url:
    if "?" in url and url.find("?") < url.find("{name}"):
        url = url.replace("{name}", urllib2.quote(name), 1)
    else:
        url = url.replace("{name}", name, 1)

Note: this will go into an infinite loop if name == "{name}" or other such corner cases.

See also

External links

This article is issued from Wikipedia - version of the 9/26/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.