1

I have the following url pattern defined (simplified for clarity) that captures the asset key (an arbitrary string) from a url:

url(r'^assets/(?P<key>.+)$', views.AssetDetail.as_view())

And my view is defined like this:

def get(self, request, key):
    ...

This becomes interesting when I make these two requests:

  1. /assets/some+key
  2. /assets/some%2Bkey

Both requests result in the view's key parameter containing the string "some+key". So, it looks like url decoding happens when the named group is captured and passed to the view, resulting in two different keys getting the same value. This is a problem because I need to distinguish between these two keys.

How do I get the key to be passed into my view raw and not decoded?

Cloud Artisans
  • 4,016
  • 3
  • 30
  • 37
  • Are you always going to be getting two keys like this, or will it be a variable amount of keys? – Jens Astrup Nov 10 '16 at 01:52
  • @Jens Astrup, this is just an example. I can have many assets with a variety of keys. – Cloud Artisans Nov 11 '16 at 03:43
  • Then you shouldn't be using url kwargs at all - there should be one value per key in the url or you'll run into the issue you're having now. A variable amount of arguments should be kept in the query parameters: `website.com/assets?some=value&key=value2` which you can access with `request.GET['some']`. – Jens Astrup Nov 11 '16 at 11:58

0 Answers0