from django.conf.urls import url
from App import views
urlpatterns = [
url(r'^hello/', views.HelloView.as_view(), name='hello'),
# url(r'^template/', views.HelloTemplateView.as_view(template_name='hello.html'), name='template'),
url(r'^template/', views.HelloTemplateView.as_view(), name='template'),
url(r'^listview/', views.HelloListView.as_view(), name='listview'),
url(r'^single/(?P<pk>\d+)/', views.HeDetailView.as_view(), name='single'),
]
from django.db import models
class Book(models.Model):
b_name = models.CharField(max_length=32)
from django.shortcuts import render
from django.views import View
from django.views.generic import TemplateView, ListView, DetailView
from App.models import Book
class HelloView(View):
def get(self, request):
return render(request, 'hello.html')
class HelloTemplateView(TemplateView):
template_name = 'hello.html'
class HelloListView(ListView):
template_name = 'BookList.html'
model = Book
class HeDetailView(DetailView):
# template_name = 'Book.html'
# model = Book
queryset = Book.objects.all()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Book</title>
</head>
<body>
<h2>{{ book.b_name }}</h2>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BookList</title>
</head>
<body>
<ul>
{% for book in object_list %}
<li><a href="{% url 'cbv:single' pk=book.id %}">{{ book.b_name }}</a></li>
{% endfor %}
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>BookDetail</title>
</head>
<body>
<h2 style="color: green">{{ book.b_name }}</h2>
</body>
</html>
Django系列:TemplateView,ListView,DetailView
原文:https://www.cnblogs.com/xidianzxm/p/12297025.html