Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
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 ourterms of service andprivacy 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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
For Django 2.0 you MUST provide `app_name` when using `namespace`.Closesencode#5659Correct namespace referenceUpdated example without changing old text.
b5be07f to3ba3f45Comparedocs/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.
carltongibsonFeb 22, 2018 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 theinclude referencehttps://docs.djangoproject.com/en/2.0/ref/urls/#include
The way I thought about phrasing this is:
- Try to
includevia an app-levelurlsmodule, setting theapp_namevariable there. Basically never use thenamespaceparameter, you basically don't want it. - If you MUST
includethe router urls directly (and need namespaces) then provide(router.urls, app_name)— that's what you want. (If you really neednamespacethen 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 thenamespace parameter. Let me re-phrase around that...
carltongibson left a comment
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.
Theinclude(( ...) 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 theapp_name argument, does ithave 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 fromurlpatterns, and the application namespace comes fromapp_name.
I've just usedapp_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 beNone 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
| 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'' aroundapp_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 "...ifnamespace=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. Althoughdo let's reference Django'sinclude docs here - I think that'd be helpful.
carltongibson commentedMar 6, 2018
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_namewhen usingnamespace.Closes#5659