Django Tutorial · Display Data

Add Link to Details

Learn all about Add Link to Details in this comprehensive tutorial.

5 min read intermediate
  • The next step in our web page will be to add a Details page, where we can list more details about a specific member.
  • The list in all_members.
  • Then create a new view in the views.

Details Template

The next step in our web page will be to add a Details page, where we can list more details about a specific member.

Start by creating a new template called details.html:

Create new View

Then create a new view in the views.py file, that will deal with incoming requests to the /details/ url: my_tennis_club/members/views.py: from django.http import HttpResponse from django.template import loader from .models import Member

def members(request): mymembers = Member.objects.all().values() template = loader.get_template('all_members.html') context = { 'mymembers': mymembers, } return HttpResponse(template.render(context, request)) def details(request, id): mymember = Member.objects.get(id=id) template = loader.get_template('details.html') context = { 'mymember': mymember, } return HttpResponse(template.render(context, request))

The details view does the following:

Gets the id as an argument. Uses the id to locate the correct record in the Member table. loads the details.html template. Creates an object containing the member. Sends the object to the template. Outputs the HTML that is rendered by the template.

Add URLs Now we need to make sure that the /details/ url points to the correct view, with id as a parameter. Open the urls.py file and add the details view to the urlpatterns list:

my_tennis_club/members/urls.py: from django.urls import path from . import views

urlpatterns = [ path('members/', views.members, name='members'), path('members/details/<int:id>', views.details, name='details'), ]

Run Example »

If you have followed all the steps on your own computer, you can see the result in your own browser: 127.0.0.1:8000/members/. If the server is down, you have to start it again with the runserver command:

python manage.py runserver

❮ Previous Next ❯

★ +1

Sign in to track progress

Module quiz

2 questions
1

Which of the following is true about Add Link to Details?

2

What is the most common pitfall when working with Add Link to Details?

Answer all questions to submit.