Implement object detail page with function view

| Tag python  django  views 
  1. We first create app’s own urls.py and put all the paths related to the app,, see [[Define app URLs under their own path]].
  2. In app’s urls.py, define a path pattern which matches the object’s detail page:
from django.urls import path
from . import views

urlpatterns = [
    path('<int:movie_id>/', views.detail, name='detail')
]
  1. Define a detail view:
from django.shortcuts import render, get_object_or_404
from .models import Movie

def detail(request, movie_id):
    movie = get_object_or_404(Movie, pk=movie_id)
    return render(request, 'detail.html', { 'movie': movie })
  1. create a template file detail.html
{% extends 'base.html' %}

{% block content %}
<div>
  <div>{{ movie.title }}</div>
  <div>{{ movie.description }}</div>
</div>
{% endblock content %}

Prev     Next