Django 2.0 modernizes Python web development. Simplified URL routing and responsive admin make building applications faster and more intuitive. At ZIRA Software, we use Django for healthcare platforms where reliability matters.
Simplified URL Routing
Before Django 2.0:
# urls.py (old regex style)
url(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
url(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive),
Django 2.0 path() function:
# urls.py (new simplified style)
from django.urls import path
urlpatterns = [
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),
]
Path Converters
Built-in converters:
# str - matches any non-empty string (excluding '/')
path('users/<str:username>/', views.profile)
# int - matches zero or positive integers
path('posts/<int:id>/', views.post_detail)
# slug - matches slug strings (letters, numbers, hyphens, underscores)
path('articles/<slug:slug>/', views.article)
# uuid - matches formatted UUID
path('objects/<uuid:uuid>/', views.object_detail)
# path - matches any non-empty string (including '/')
path('files/<path:filepath>/', views.serve_file)
Custom Path Converters
# converters.py
class FourDigitYearConverter:
regex = '[0-9]{4}'
def to_python(self, value):
return int(value)
def to_url(self, value):
return '%04d' % value
# urls.py
from django.urls import path, register_converter
from . import converters, views
register_converter(converters.FourDigitYearConverter, 'yyyy')
urlpatterns = [
path('articles/<yyyy:year>/', views.year_archive),
]
Mobile-Responsive Admin
Automatic responsive design:
- Touch-friendly interface
- Mobile-optimized layouts
- Tablet support
- Responsive tables and forms
# admin.py
from django.contrib import admin
from .models import Article
@admin.register(Article)
class ArticleAdmin(admin.ModelAdmin):
list_display = ['title', 'author', 'published_date', 'status']
list_filter = ['status', 'published_date']
search_fields = ['title', 'content']
# Works beautifully on mobile and desktop
Window Functions
Database-level aggregation:
from django.db.models import F, Window
from django.db.models.functions import RowNumber
# Add row numbers partitioned by category
Article.objects.annotate(
row_number=Window(
expression=RowNumber(),
partition_by=[F('category')],
order_by=F('published_date').desc()
)
)
Migrations
Python 3.6+ required: Django 2.0 drops Python 2 support, embracing modern Python features.
# Check compatibility
python --version # Should be 3.6+
# Upgrade Django
pip install --upgrade django==2.0
Model Changes
Simplified related object creation:
# models.py
class Author(models.Model):
name = models.CharField(max_length=100)
class Article(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
title = models.CharField(max_length=200)
# Now on_delete is required (was optional before)
Conclusion
Django 2.0 modernizes URL routing and improves developer experience. The responsive admin and simplified syntax make Django more accessible.
Building with Django? Contact ZIRA Software for Python/Django development services.