0 day streak
python / beginner
Snippet

Capturing URL Parameters

Django views can accept arguments that match variable names defined in your URL configuration, allowing for dynamic pages.

snippet.py
python
1
2
3
4
5
from django.http import HttpResponse
def user_profile(request, username):
# The 'username' comes directly from the URL path
return HttpResponse(f"Welcome to the profile of {username}!")
django
Breakdown
1
def user_profile(request, username):
The function receives the HTTP request and the captured string from the URL.
2
f"...{username}!"
Uses a Python f-string to inject the variable into the response text.