Route & View

In Django term, routing is the defining of «URLconfig» in the module urls.py. Moreover, all routes are linked to different controller or «view» in Django term. Conventinally, all controllers or views are sotred in a module called «views.py». In fact, to create a route linked to a function that would run when we open the home page of our Django app, we should write the code as below:

#mysite/urls.py
from django.urls import path

from . import views

urlpatterns = [
    path('', views.index, name='index'),
]
#mysite/views.py
from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello World!")

In sum, routing in Django could be done as below:

from django.urls import path, re_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),
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
]

GitHub: https://github.com/Sokhavuth/django
Heroku: https://khmerweb-django.herokuapp.com/