shawnjan
shawnjan

Reputation: 959

Django URL regex question

I had a quick question about Django URL configuration, and I guess REGEX as well. I have a small configuration application that takes in environments and artifacts in the following way:

url(r'^env/(?P<env>\w+)/artifact/(?P<artifact>\w+)/$', 'config.views.ipview', name="bothlist"),

Now, this works fine, but what I would like to do is have it be able to have additional parameters that are optional, such as a verbose mode or no formating mode. I know how to do this just fine in the views, but I can't wrap my head around the regex.

the call would be something like

GET /env/<env>/artifact/<artifact>/<opt:verbose>/<opt:noformat>

Any help would be appreciated, thanks!

-Shawn

Upvotes: 1

Views: 466

Answers (1)

Felix Kling
Felix Kling

Reputation: 816232

I wouldn't put such options into the URL. As you said, these are optional options, they might only change the output. They don't belong in an URL.

Your initial regex should match URLs like:

/env/<env>/artifact/<artifact>?verbose=1&noformat=1

Imho this is a much better usage of URLs

Upvotes: 9

Related Questions