</>
Skip to content
Django lessons (9/43)

Django — URLs

Basic URL Pattern

# mysite/urls.py
from django.contrib import admin
from django.urls import path
from blog import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
]

Path Converters

ConverterDescriptionExample
strString (default)/blog/hello/
intInteger/blog/42/
slugSlug string/blog/my-post/
uuidUUID/blog/550e8400-e29b-41d4-a716-446655440000/
path('blog/<int:pk>/', views.post_detail, name='post_detail'),
path('blog/<slug:slug>/', views.post_by_slug, name='post_by_slug'),

Include App URLs

# mysite/urls.py
urlpatterns = [
    path('blog/', include('blog.urls')),
]

# blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.post_list, name='post_list'),
    path('<int:pk>/', views.post_detail, name='post_detail'),
]

Named URLs

# In templates
<a href="{% url 'post_detail' pk=1 %}">Read More</a>

# In views
from django.shortcuts import redirect
return redirect('post_detail', pk=1)

Regex Patterns

from django.urls import re_path
re_path(r'^blog/(?P<year>[0-9]{4})/$', views.year_archive),

Mini Practice

  1. Create basic URL patterns
  2. Use path converters
  3. Include app URLs
  4. Use named URLs in templates

Up Next

Continue with Views — handling HTTP requests.

Related Topics

Frequently Asked Questions about URLs

What is URLs in Django?

URLs is a fundamental concept in Django. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn URLs?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn URLs.

Why is URLs important in Django?

URLs is essential for Django development. Understanding this concept will help you write better code and solve real-world problems more effectively.