GET URL Parameters

GET URL Parameters

URL parameters are part of the URL path itself.
Django captures these values through the URL configuration and passes them to the view.

 

Example of a URL parameter:
jaimedcsilva.com/value1/

Example of a query parameter URL:
jaimedcsilva.com/?param=value1
 

URL configuration

from django.urls import path
from .views import *

urlpatterns = [
    path("<str:param>/", index.as_view(), name="index"),
]

 

Template example

...
<a href="{% url 'index' 'value1' %}"> Value 1 </a>
<a href="{% url 'index' 'value2' %}"> Value 2 </a>

 

Retrieving the parameter in the backend

from django.views.generic import TemplateView

class index(TemplateView):
    template_name = 'polls/index.html'


    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        param = kwargs.get('param')

        print(param)

        context['param'] = param
        context['message'] = "Some message from the server!"

        return context

Tested with Django 4.2