-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Corrected docs on router include with namespaces. #5843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Corrected docs on router include with namespaces. #5843
Conversation
For Django 2.0 you MUST provide `app_name` when using `namespace`. Closes encode#5659 Correct namespace reference Updated example without changing old text.
b5be07f
to
3ba3f45
Compare
docs/api-guide/routers.md
Outdated
Router URL patterns can also be included using _application_ and _instance_ namespaces. | ||
|
||
To use an _application namespace_ pass a 2-tuple containing the `router.urls` and the app name. | ||
To use an _instance namespace_ pass the optional `namespace` parameter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we reference anywhere in the Django docs here? I wouldn't know the difference between the two off the top of my head.
I guess ideally we'd just say "Do X", and have "If you need to do Y then ..." slightly later in the docs, rather than presenting the developer with a choice that they need to make here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The [URL namespaces docs]
reference below is the canonical source. It's basically the only place it's ever discussed.
The other place we could link to is the include
reference https://docs.djangoproject.com/en/2.0/ref/urls/#include
The way I thought about phrasing this is:
- Try to
include
via an app-levelurls
module, setting theapp_name
variable there. Basically never use thenamespace
parameter, you basically don't want it. - If you MUST
include
the router urls directly (and need namespaces) then provide(router.urls, app_name)
— that's what you want. (If you really neednamespace
then go-ahead, but you still have to pass the 2-tuple first.)
But by the time I'd drafted than I basically need to rewrite the Django docs (linked) so people should really read those.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The >80% case is to just pass the 2-tuple and ignore the namespace
parameter. Let me re-phrase around that...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @tomchristie.
I adjusted the phrasing. Have a look.
(It's tricky because the whole application vs instance thing isn't clearly represented by the API...)
docs/api-guide/routers.md
Outdated
|
||
urlpatterns = [ | ||
url(r'^forgot-password/$', ForgotPasswordFormView.as_view()), | ||
url(r'^api/', include(router.urls, namespace='api')), | ||
url(r'^api/', include((router.urls, 'app_name')), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the >80% usage for namespaces. (We want to namespace our URL names to a single application.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The include((
... )
looks wrong to me?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm confused by the app_name
argument, does it have to match the app name, or is it a general purpose namespace?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The parenthesis is missing. I'll fix that.
SO... 😃
What ultimately gets passed included is a 3-tuple:
(A_LIST_OF_PATTERNS, AN_APPLICATION_NAMESPACE, AN_INSTANCE_NAMESPACE)
In the example here we pass the first two of those, as a 2-tuple.
When you include a module the list of patterns comes from urlpatterns
, and the application namespace comes from app_name
.
I've just used app_name
here as any old string. It's usually, 'admin'
, or 'polls'
or 'rest_framework'
.
In this example the instance namespace ends up being the same as the application namespace, because we didn't provide one. (This means that it'll be None
if the application namespace is, which is just the global namespace we all start out with.)
But we could do provide, say, namespace='v1'
and such.
Grrr. I'm still not happy here.
docs/api-guide/routers.md
Outdated
If using namespacing with hyperlinked serializers you'll also need to ensure that any `view_name` parameters on the serializers correctly reflect the namespace. In the example above you'd need to include a parameter such as `view_name='api:user-detail'` for serializer fields hyperlinked to the user detail view. | ||
(`include` also allows an optional `namespace` parameter to also create an _instance namespace_. | ||
This defaults to `None` and will be set to `app_name` if not provided. | ||
See Django's [URL namespaces docs][url-namespace-docs] for more details.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we even need to say this? Not sure...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks better!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and will be set to
app_name
...
Grrrr. "...the same as the application namespace..." or such.
Don't merge this yet. I'll have a go over the weekend.
docs/api-guide/routers.md
Outdated
@@ -72,14 +72,21 @@ Alternatively you can use Django's `include` function, like so… | |||
url(r'^', include(router.urls)), | |||
] | |||
|
|||
Router URL patterns can also be namespaces. | |||
To include the router URL patterns with an application namespace pass a | |||
`(router.urls, 'app_name')` 2-tuple to `include`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added the ''
around app_name
here. Hopefully that works.
The option is something like (PATTERN_LIST, APP_NAME)
docs/api-guide/routers.md
Outdated
] | ||
|
||
If using namespacing with hyperlinked serializers you'll also need to ensure that any `view_name` parameters on the serializers correctly reflect the namespace. In the example above you'd need to include a parameter such as `view_name='api:user-detail'` for serializer fields hyperlinked to the user detail view. | ||
(You may additionally pass an optional `namespace` parameter to `include` to also | ||
create an _instance namespace_. See Django's [URL namespaces docs][url-namespace-docs] for more details.) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe if I drop the whole "...if namespace=None
bit... and just let people read the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. Although do let's reference Django's include
docs here - I think that'd be helpful.
Hey @tomchristie. Can I ask you to pass your eyes over this again.
|
* Provide both app and instance namespace examples * Emphasise non-namespaced option
For Django 2.0 you MUST provide
app_name
when usingnamespace
.Closes #5659