1

So i have two very similar endpoint URLs:

 url(r'^users/(?P<username>.+)/$', user_detail, name='user_detail'), url(r'^users/(?P<username>.+)/followers/$', follow_view, name='follow'), 

In that order, i get a HTTP 405 (Method Not Allowed), but when the order is changed (/followers on top) i dont get the error, but i get it now in the second endpoint.

The respective api_views have the respective allowed method list, for example:

@api_view(['POST', 'GET', 'DELETE']) @authentication_classes((BasicAuthentication,)) @permission_classes((IsAuthenticated,)) def follow_view(request, username, format=None): 

What can i do to make this URL Conf work? Thanks!

0

3 Answers 3

2

Your regular expression causing the weird behaviour of URL Dispatcher. .+ will match any character except a newline, which includes slash (/) also.
When you changed the order of url(), got expected output, because, as soon as a matching url pattern is found, the dispatcher will stop searching through the url patterns and will dispatch immediately.
So what you have to do is change your regex to something like this, \w+ which is enough for a username matching

Preferable url()

url(r'^users/(?P<username>\w+)/$', user_detail, name='user_detail'), url(r'^users/(?P<username>\w+)/followers/$', follow_view, name='follow'), 
Sign up to request clarification or add additional context in comments.

1 Comment

He wrote that evrething is OK. His question is about why this happens
0

I think rest framework handle your request like a request to the first view because he thinks that all what you have after users/ is username. Try to remove slash from allowed characters for username

Comments

0

You can try using this:

url(r'^users/(?P<username>\w+)/$', views.user_detail, name='user_detail'), url(r'^users/(?P<username>\w+)/followers/$', views.follow_view, name='follow'), 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.