How can i get id/pk from a url

I am a Django newbie trying to create a flight booking view that will take logged in user id and the pk/id of the selected flight so i can combine the data and populate a ticket for the corresponding user and the selected flight. I don’t have any idea how to make this work, i have tried searching for answers and no luck. Any kind of help would be appreciated.

in views.py

from django.shortcuts import render
def test(request, Id, name):
    return render(
        request,
        "app2/test.html",
        {
            "id": Id,
            "name": name,
        },
    )

in urls.py

from django.urls import path

from . import views

urlpatterns = [
    path("test/<int:Id>/<str:name>", views.test, name="test"),
]

test.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Test</title>
</head>
<body>
    <div>Id: {{ Id }}</div>
    <div>Name: {{ name|title }}</div>
</body>
</html>

Now visit 127.0.0.1:8000/.../test/1/ShahSawood
will show in the html page test.html

Back to Top