All technological notes.
urls functionspath()Returns an element for inclusion in urlpatterns.
Syntax:
path(route, view, kwargs=None, name=None)Parameter:
route: a string containing a URL patternview: a view function / django.urls.include()kwargs: additional arguments to the view function or method.
{"foo": "bar"}Example:
from django.urls import include, path
urlpatterns = [
path("index/", views.index, name="main-view"),
path("bio/<username>/", views.bio, name="bio"), # capture value passed to view
path("blog/", include("blog.urls")), # include()
]
re_path()Returns an element for inclusion in urlpatterns.
Syntax:
re_path(route, view, kwargs=None, name=None)Parameter:
route: a string with a regular expression
r'')view: a view function / django.urls.include()kwargs: additional arguments to the view function or method.
{"foo": "bar"}Example:
from django.urls import include, re_path
urlpatterns = [
re_path(r"^index/$", views.index, name="index"),
re_path(r"^bio/(?P<username>\w+)/$", views.bio, name="bio"),
re_path(r"^blog/", include("blog.urls")),
...,
]
include()takes a full Python import path to another URLconf module that should be “included” in this place.
Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
Syntax:
include(pattern_list)
include(module, namespace=None)
include((pattern_list, app_namespace), namespace=None)
Parameters:
module:URLconf module (or module name)namespace (str):Instance namespace for the URL entries being includedpattern_list:Iterable of path() and/or re_path() instances.app_namespace (str):Application namespace for the URL entries being includedExample:
from django.urls import include, path
urlpatterns = [
path("community/", include("aggregator.urls")), # module
path("contact/", include("contact.urls")),
]
reverse()If no match can be made, reverse() raises a NoReverseMatch exception
Syntax:
reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)Parameter:
viewname: can be a URL pattern name or the callable view object.urlconf: the URLconf module containing the URL patterns to use for reversing.
root URLconf for the current thread is used.args: URL arguments of list typekwargs: URL arguments of dict typeExample:
# url
from news import views
path("archive/", views.archive, name="news-archive")
# above url can be reverse as follow:
reverse("news-archive") # using the named URL
from news import views
reverse(views.archive) # passing a callable object
# use case with redirect http
from django.urls import reverse
def myview(request):
return HttpResponseRedirect(reverse("arch-summary", args=[1945])) # using args to pass value
# using kwargs
reverse("admin:app_list", kwargs={"app_label": "auth"}) # '/admin/auth/'