Skip to content
Django
11. Django Files Cheatsheet
URLs

urls

  • URLs also known as Routs.
  • /about-us are known as slug or routing.
  • https://www.wikipedia.com/ shows the landing page (main routing).
  • https://www.wikipedia.com/blog/ shows list of all post (Inner routing).
  • https://www.wikipedia.com/blog/single-post/ shows specific post (Detail page routing).

1. Basic urls

urls.py
from django.contrib import admin
from django.urls import path, include
from . import views
 
from django.conf import settings
from django.conf.urls.static import static
 
urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name='index'), # home / index / root
    path('blog/', include('blog.urls', namespace='blog'), name='blog'),
    # your paths are here ...
]
 
# To use media files you have to install - pip install Pillow
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
  1. first parameter is the url path.
  2. second parameter is the function name from views.py file.
  3. third parameter is the name of the page, that you can use in html file.

2. Dynamic urls/routs

  • It can be -
    • int : 8
    • str : page.html
    • slug : hello-simple-post.html
  • Ex:

In urls.py :

urls.py
path('course/<int:courseID', views.course)
path('course/<str:courseName', views.course)
path('course/<slug:courseDetails', views.course)

In views.py :

views.py
def courseDetails(request, courseID):
    return HttpResponse(courseID)