Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
635dc035
Commit
635dc035
authored
Jan 19, 2017
by
Luca
Committed by
Tom Christie
Jan 19, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Parametrizable viewset custom views reverse name (#4821)
parent
8844082d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
1 deletions
+22
-1
docs/api-guide/routers.md
+20
-0
rest_framework/routers.py
+2
-1
No files found.
docs/api-guide/routers.md
View file @
635dc035
...
...
@@ -118,6 +118,26 @@ The above example would now generate the following URL pattern:
*
URL pattern:
`^users/{pk}/change-password/$`
Name:
`'user-change-password'`
In the case you do not want to use the default name generated for your custom action, you can use the url_name parameter to customize it.
For example, if you want to change the name of our custom action to
`'user-change-password'`
, you could write:
from myapp.permissions import IsAdminOrIsSelf
from rest_framework.decorators import detail_route
class UserViewSet(ModelViewSet):
...
@detail_route(methods=['post'], permission_classes=[IsAdminOrIsSelf], url_name='change-password')
def set_password(self, request, pk=None):
...
The above example would now generate the following URL pattern:
*
URL pattern:
`^users/{pk}/set_password/$`
Name:
`'user-change-password'`
You can also use url_path and url_name parameters together to obtain extra control on URL generation for custom views.
For more information see the viewset documentation on
[
marking extra actions for routing
][
route-decorators
]
.
# API Guide
...
...
rest_framework/routers.py
View file @
635dc035
...
...
@@ -179,10 +179,11 @@ class SimpleRouter(BaseRouter):
initkwargs
=
route
.
initkwargs
.
copy
()
initkwargs
.
update
(
method_kwargs
)
url_path
=
initkwargs
.
pop
(
"url_path"
,
None
)
or
methodname
url_name
=
initkwargs
.
pop
(
"url_name"
,
None
)
or
url_path
ret
.
append
(
Route
(
url
=
replace_methodname
(
route
.
url
,
url_path
),
mapping
=
{
httpmethod
:
methodname
for
httpmethod
in
httpmethods
},
name
=
replace_methodname
(
route
.
name
,
url_
path
),
name
=
replace_methodname
(
route
.
name
,
url_
name
),
initkwargs
=
initkwargs
,
))
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment