Raw Parameter Values

During URL resolution, DMP populates the request.dmp.urlparams[ ] list with all URL parts after the first two parts (/homepage/index/), up to the ? (query string). For example, the URL /homepage/index/144/A58UX/ has two urlparams: 144 and A58UX. These can be accessed as request.dmp.urlparams[0] and request.dmp.urlparams[1] throughout your view function.

Empty parameters and trailing slashes are handled in a specific way. The following table gives examples:

/homepage/index/first/second/ request.urlparam = [ 'first', 'second' ]
/homepage/index/first/second request.urlparam = [ 'first', 'second' ]
/homepage/index/first// request.urlparam = [ 'first', '' ]
/homepage/index request.urlparam = [ ]

In the examples above, the first and second URL result in the same list, even though the first URL has an ending slash. The ending slash is optional and can be used to make the URL prettier.

The ending slash is optional because DMP’s default urls.py patterns ignore it. If you define custom URL patterns instead of including the default ones, be sure to add the ending /? (unless you explicitly want the slash to be explicitly counted).

In the Python language, the empty string and None have a special relationship. The two are separate concepts with different meanings, but both evaluate to False, acting the same in the truthy statement: if not mystr:.

Denoting “empty” parameters in the url is uncertain because:

  1. Unless told otherwise, many web servers compact double slashes into single slashes. http://localhost:8000/storefront/receipt//second/ becomes http://localhost:8000/storefront/receipt/second/, preventing you from ever seeing the empty first paramter.
  2. There is no real concept of “None” in a URL, only an empty string or some character denoting the absence of value.

Because of these difficulties, the urlparams list is programmed to never return None and never raise IndexError. Even in a short URL with only a few parameters, accessing request.dmp.urlparams[50] returns an empty string.

For this reason, the default converters for booleans and Models objects equate the empty string and dash ‘-‘ as the token for False and None, respectively. The single dash is especially useful because it provides a character in the URL (so your web server doesn’t compact that position) and explicitly states the value. Your custom converters can override this behavior, but be sure to check for the empty string in request.dmp.urlparams instead of None.