# Step 1: Import necessary modules
from django.urls import path
from . import views
# Step 2: Define URL patterns
urlpatterns = [
# Example 1: Basic path with a function-based view
path('example1/', views.example1_view, name='example1'),
# Example 2: Path with a dynamic parameter
path('example2/<int:param>/', views.example2_view, name='example2'),
# Example 3: Path with multiple dynamic parameters
path('example3/<slug:param1>/<str:param2>/', views.example3_view, name='example3'),
# Example 4: Path with optional parameter
path('example4/', views.example4_view, name='example4'),
# Example 5: Path with additional parameter constraints
path('example5/<path:param>/', views.example5_view, name='example5'),
# Example 6: Path with custom converter
path('example6/<uuid:param>/', views.example6_view, name='example6'),
# Example 7: Path with regular expression pattern
path('example7/<int:param>/', views.example7_view, name='example7'),
# Example 8: Include other URL patterns
path('app/', include('myapp.urls')),
# Example 9: Redirect from one URL to another
path('old/', views.old_url, name='old_url'),
path('new/', views.new_url),
]
# Step 3: Include the application's URL patterns in the project's main URLs
# In the main project's urls.py file, add:
# from django.urls import include
# urlpatterns = [
# path('myapp/', include('myapp.urls')),
# # other paths...
# ]
# views.py
from django.shortcuts import render
# Example 1: Basic function-based view
def example1_view(request):
return render(request, 'example1.html')
# Example 2: Function-based view with dynamic parameter
def example2_view(request, param):
return render(request, 'example2.html', {'param': param})
# Example 3: Function-based view with multiple dynamic parameters
def example3_view(request, param1, param2):
return render(request, 'example3.html', {'param1': param1, 'param2': param2})
# Example 4: Function-based view with optional parameter
def example4_view(request):
return render(request, 'example4.html')
# Example 5: Function-based view with additional parameter constraints
def example5_view(request, param):
return render(request, 'example5.html', {'param': param})
# Example 6: Function-based view with custom converter
def example6_view(request, param):
return render(request, 'example6.html', {'param': param})
# Example 7: Function-based view with regular expression pattern
def example7_view(request, param):
return render(request, 'example7.html', {'param': param})
# Example 8: View to redirect from old URL to new URL
def old_url(request):
return redirect('new_url')
# Example 9: View for the new URL after redirection
def new_url(request):
return render(request, 'new_url.html')
<!-- templates/example1.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example 1</title>
</head>
<body>
<h1>Example 1</h1>
<!-- Content for Example 1 -->
</body>
</html>
<!-- templates/example2.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example 2</title>
</head>
<body>
<h1>Example 2</h1>
<p>Parameter value: {{ param }}</p>
</body>
</html>
<!-- templates/example3.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example 3</title>
</head>
<body>
<h1>Example 3</h1>
<p>Parameter 1: {{ param1 }}</p>
<p>Parameter 2: {{ param2 }}</p>
</body>
</html>
<!-- templates/example4.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example 4</title>
</head>
<body>
<h1>Example 4</h1>
<!-- Content for Example 4 -->
</body>
</html>
<!-- templates/example5.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example 5</title>
</head>
<body>
<h1>Example 5</h1>
<p>Parameter value: {{ param }}</p>
</body>
</html>
<!-- templates/example6.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example 6</title>
</head>
<body>
<h1>Example 6</h1>
<p>Parameter value: {{ param }}</p>
</body>
</html>
<!-- templates/example7.html -->
<!DOCTYPE html>
<html>
<head>
<title>Example 7</title>
</head>
<body>
<h1>Example 7</h1>
<p>Parameter value: {{ param }}</p>
</body>
</html>
<!-- templates/new_url.html -->
<!DOCTYPE html>
<html>
<head>
<title>New URL</title>
</head>
<body>
<h1>New URL</h1>
<!-- Content for the new URL -->
</body>
</html>