Named URL Patterns

A snippet showing how to name Django URL patterns.


# models.py

from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=120)
      

# admin.py

from django.contrib import admin
from .models import Post

@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
    list_display = ("title",)
      

# urls.py
from django.urls import path
from . import views
urlpatterns = [
    path("posts/", views.post_list, name="post-list"),
]

# template.html
<a href="{% url 'post-list' %}">All Posts</a>
      
Explanation:
  • Always give URL patterns a name so they're easy to reverse in views and templates.
  • Names decouple links from hard-coded paths.
  • Category URLs & Routing
  • Total Views 785
  • Last Modified 26 February, 2026
  • Tags #urls #names #reverse #routing
Previous snippet
Include Other URLconfs
Next snippet
URL Namespaces Example
Never miss a story on Django.wiki

Subscribe for fresh tutorials, snippets, and updates.

By subscribing you agree to our Privacy Policy.