All technological notes.
URLconfURLconf / URL configuration:
ROOT_URLCONF setting, but if the incoming HttpRequest object has a urlconf attribute (set by middleware), its value will be used in place of the ROOT_URLCONF setting.django.urls.path() and/or django.urls.re_path() instances.URL, matching against path_info.django.urls.path() or django.urls.re_path().error-handling view.The URLconf searches against the requested URL, as a normal Python string.
This does not include GET or POST parameters, or the domain name.
For example, in a request to https://www.example.com/myapp/, the URLconf will look for myapp/.
In a request to https://www.example.com/myapp/?page=3, the URLconf will look for myapp/.
The URLconf doesn’t look at the request method.
POST, GET, HEAD, etc. – will be routed to the same function for the same URL.URLconffrom django.urls import path
from . import views
urlpatterns = [
path("articles/2003/", views.special_case_2003),
path("articles/<int:year>/", views.year_archive),
path("articles/<int:year>/<int:month>/", views.month_archive),
path("articles/<int:year>/<int:month>/<slug:slug>/", views.article_detail),
]
Notes:
<int:year><int:name> to capture an integer parameter.articles, not /articles.Example requests:
/articles/2005/03/ would match the third entry in the list. Django would call the function views.month_archive(request, year=2005, month=3)./articles/2003/ would match the first pattern in the list, not the second one, because the patterns are tested in order, and the first one is the first test to pass. Here, Django would call the function views.special_case_2003(request)./articles/2003/03/building-a-django-site/ would match the final pattern. Django would call the function views.article_detail(request, year=2003, month=3, slug="building-a-django-site")./articles/2003 would not match any of these patterns, because each pattern requires that the URL end with a slash.urls.pya .py file to store URLconf variable urlpatterns.
urlpatterns should be a sequence of path() and/or re_path() instances.
URL namespaces
application namespace
instance namespace
: operator.
'admin:index': indicates a namespace of ‘admin’, and a named URL of ‘index’.'sports:polls:index': look for a pattern named ‘index’ in the namespace ‘polls’ that is itself defined within the top-level namespace ‘sports’.' app_name:url' , 'app_name:url ', 'app_name :url', or 'app_name: url'都不能匹配'app_name:url'app_name = '<app_name>'urls.py at the project level# both url refering to the same urls.py file.
# But due to the defferent namespace, urls have different prefix.
path('employee/', include("EmpApp.urls",namespace="employee")),
path('department/', include("EmpApp.urls",namespace="department")),
urls.py within EmpApp applicationapp_name = "EmpApp"
urlpatterns = [
path("", view=views.emp_list, name="index"),
path("emp/list/", view=views.emp_list, name="emp_list"),
path("emp/update/<int:emp_id>", view=views.emp_update, name="emp_update"),
path("dept/list/", view=views.dept_list, name="dept_list"),
path("dept/update/<int:dept_id>", view=views.dept_update, name="dept_update"),
]
<!--
Using `employee:url`, to reverese the url as '/employee/'.
Then further into '/employee/emp/list/
-->
<a href="-/ url 'employee:emp_list' -\">List</a>
<!--
Using `department:url`, to reverese the url as '/department/'.
Then further into '/department/dept/list/
-->
<a href="-/ url 'department:dept_list' -\">List</a>
<!--
EmpApp is an application namespace which defined in the EmpApp's urls.py.
Djnago locate this url file to find emp_detail url
-->
<a href="-/ url 'EmpApp:emp_detail' emp.id -\">Edit</a>
<a href='/admin/'>Admin</a>admin:
<a href='-/ url 'admin:index' -\'>Admin</a>