Django difference in APP_NAME (namespace application) and namespace (Example namespace)

Transfer: https: //www.jianshu.com/p/404500a0408a

Supplementary understand:

First explained the official website of the application namespace (app_name) and instance namespace (namespace) of paste:

APP_NAME (namespace application) is typically specified in app.urls module, such as:

= APP_NAME " Test " // application Namespace 
the urlpatterns = [ 
    path ( " article1 / " , views.test, name = " url_a " ), 
]

 

namespace (namespace example), generally designated at ROOT_URLCONF module (root url routing module), as the following: namespace = "test1", namespace = "test2", namespace = "test3"

= the urlpatterns [// belonging to three examples app_name = "test" application namespace namespace
re_path(r"^test_u1/", include("test_app.urls", namespace="test1")), 
re_path(r
"^test_u2/", include("test_app.urls", namespace="test2")),
re_path(r
"^test_u3/", include("test_app.urls", namespace="test3")),
]

urlpatterns list above three url configured with different namespace (namespace example), but they are routed to test_app.urls, and specified app_name = "test" (Application namespace) in test_app.urls, in which case , pointing to the test on behalf of one of its three namespace, as to which test specific namespace on behalf of, depends on the current application (current application) which, for example:

Received from the server http: // localhost: 8000 / test_u2 / article1 / request url which matches the second list, this time namespace = "test2" of the application is called the current application (current application), this time app_name = "test" in this test represents an example of a current application corresponding to the namespace, the template tag {% url "test: url_a"%} of the test represents the test2, equivalent to {% url "test2 : url_a "%}, so that the url is parsed reverse: / test_u2 / article1 /, if from http: // localhost: 8000 / test_u1 / article1 / request, then the namespace =" test1 "application called the current application, test represents the test1. In short, app_name can represent many instances namespace, which represents the concrete, which depends on the current application.

Suppose there index.html template tag {% url "test: url_a"%}, from which point there is http: // localhost: 8000 / app01 / index / a request requires access to index.html, url this not being accessed any one of three belonging to the namespace, also said that there is no current application, it will default to point urlpatterns app_name last namespace that is test3, then index.html in the {% url "test: url_a"%} template tag , is equivalent to {% url "test3: url_a"%}. If the namespace name and the same app_name, app_name it points to the default namespace.

 

Guess you like

Origin www.cnblogs.com/olivertian/p/10974281.html