类视图(ListView,DetailView)

需求:- 自己使用ListView,DetailView
书籍列表
点击列表显示书的详情

源码解析ListView

继承自MultipleObjectTemplateResponseMixin TemplateResponseMixin

BaseListView
MultipleObjectMixin
ContextMixin
View
dispatch

App/urls.py
urlpatterns = [
    url(r'^hello/', views.HelloListView.as_view()),
    url(r"^books/(?P<pk>\d+)", views.BookDetailView.as_view(), name='book_detail'),
]
App/views.py
from django.shortcuts import render
from django.views.generic import ListView, DetailView

from App.models import Book


class HelloListView(ListView):

    template_name = 'hello_list.html'

    queryset = Book.objects.all()


class BookDetailView(DetailView):

    template_name = 'book_detail.html'

    queryset = Book.objects.all()

templates/book_list.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul>
    {% for book in object_list %}
        <li><a href="{% url 'app:book_detail' pk=book.id %}">{{ book.b_name }}</a></li>
    {% endfor %}
</ul>


</body>
</html>
templates/list.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<ul>
    {% for book in object_list %}
        <li><a href="{% url 'app:book_detail' pk=book.id %}">{{ book.b_name }}</a></li>
    {% endfor %}
</ul>


</body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容