Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Django - URL Dispatcher

Back


URLconf


How Django processes a request


What the URLconf searches against


Sample URLconf

from 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),
]

urls.py


URL namespaces


Example: Namespace

# 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")),
app_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>

Example: Admin page


TOP