Django urls - trailing slash appends to value variable

I have a django application hosted with Apache. I am using django restframework to create an API, but I am having problems with the urls. As an example, I have a URL like this:

url(r'path/to/endpoint/(?P<db_id>.+)/$', views.PathDetail.as_view())

If I try to access this URL and do not include the trailing slash, it will not match. If I add a question mark to the end as follows:

url(r'path/to/endpoint/(?P<db_id>.+)/?', views.PathDetail.as_view())

This corresponds to a slash and without it. The only problem is that if a trailing slash is used, it is now included in the db_id variable in my opinion. Therefore, when it searches for a database, the identifier does not match. I don't want to go through all my looks and remove trailing slashes from my url variables using string handling.

So my question is: what is the best way to make the URL match both the trailing slash and without it, without including the slash in the parameter that is sent to the view?

+3
source share
2 answers

Your template for a parameter .+, which means 1 or more characters, including /. No wonder slash is included, why not?

, -, /, [^/]+. , , , .*[^/] .

+5

.+ . "", , .

: http://www.regular-expressions.info/repeat.html.

/ .

, , , .

, db_id ( ), -, , .

, db_id ?: (?P<db_id>.+?)/? : (?P<db_id>[^/]+)/?

+4

All Articles