使用通用视图的Tips
提供了什么功能
数据从哪里来
提供了哪些模板变量
views.py
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
# Create your views here.
class Chello(View):
def get(self, request):
return JsonResponse({'python': '这是get请求'})
def post(self, request):
return JsonResponse({'python': '这是post请求'})
urls.py
from django.urls import path
from .views import Chello
urlpatterns = [
path('Chello/', Chello.as_view()),
# path中的第二个参数应该是一个函数,所以 as_view()是将 Chello 这个类转换成函数
]
list view的使用
modeles.py
from django.db import models
# Create your models here.
class Something(models.Model):
msg = models.CharField(max_length=200)
is_delete = models.BooleanField()
迁移
(class15env) PS D:\muyi\Django\muyi> python ./manage.py makemigrations
Migrations for 'the_10':
the_10\migrations\0001_initial.py
- Create model Something
(class15env) PS D:\muyi\Django\muyi> python .\manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, the_10, the_5, the_7, the_8, the_9
Running migrations:
Applying the_10.0001_initial... OK
(class15env) PS D:\muyi\Django\muyi>
admin.py
from django.contrib import admin
from the_10.models import Something
# Register your models here.
admin.site.register(Something)
后台添加数据
views.py
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from django.views.generic import ListView
from the_10.models import Something
# Create your views here.
class Chello(View):
def get(self, request):
return JsonResponse({'python': '这是get请求'})
def post(self, request):
return JsonResponse({'python': '这是post请求'})
class Mylistviews(ListView):
# model = Something # 读取所有数据
# queryset = Something.objects.all() # 读取所有数据
def get_queryset(self): # 自定义查询字段
queryset = Something.objects.filter(is_delete=0)
return queryset
urls.py
from django.urls import path
from .views import Chello, Mylistviews
urlpatterns = [
path('Chello/', Chello.as_view()),
# path中的第二个参数应该是一个函数,所以 as_view()是将 Chello 这个类转换成函数
path('Mylistviews/', Mylistviews.as_view())
]
Something_list.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>这是 Somthing_list的页面</h1>
<ul>
{% for i in object_list %}
<li>{{i.msg}}</li>
{% endfor %}
</ul>
</body>
</html>
效果
提供了什么功能
以列表形式展示数据的功能
数据从哪里来
数据从数据库查询得来
model
model = Something
queryset
queryset = Something.objects.all()
get_queryset
def get_queryset(self): # 自定义查询字段
queryset = Something.objects.filter(is_delete=0)
return queryset
提供了那些模板变量
object_list
<ul>
{% for i in object_list %}
<li>{{i.msg}}</li>
{% endfor %}
</ul>
自定义
context_object_name = 'muyi'
<ul>
{% for i in muyi %}
<li>{{i.msg}}</li>
{% endfor %}
</ul>
something_list
<ul>
{% for i in something_list %}
<li>{{i.msg}}</li>
{% endfor %}
</ul>
渲染的模板页
默认
模型名的小写_list.html
自定义
template_name = 'the_10/haoke.html'
分页
views.py
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from django.views.generic import ListView
from the_10.models import Something
# Create your views here.
class Chello(View):
def get(self, request):
return JsonResponse({'python': '这是get请求'})
def post(self, request):
return JsonResponse({'python': '这是post请求'})
class Mylistviews(ListView):
# 标页数 —— 两行一页
paginate_by = 2
# model = Something # 读取所有数据
# queryset = Something.objects.all() # 读取所有数据
def get_queryset(self): # 自定义查询字段
queryset = Something.objects.filter(is_delete=0)
return queryset
# 自定义模板变量名
context_object_name = 'muyi'
# 自定义渲染模板名
template_name = 'the_10/haoke.html'
urls.py
from django.urls import path
from .views import Chello, Mylistviews
urlpatterns = [
path('Chello/', Chello.as_view()),
# path中的第二个参数应该是一个函数,所以 as_view()是将 Chello 这个类转换成函数
path('Mylistviews/', Mylistviews.as_view(), name = 'something')
]
haoke.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>这是 haoke 的页面</h1>
<ul>
{% for i in muyi %}
<li>{{i.msg}}</li>
{% endfor %}
</ul>
{% for page in paginator %}
{# ?page={{ page.number }} 点击几就显示第几页#}
<a href="{% url 'something' %}?page={{ page.number }}" >
{# 分几页#}
{{ page.number }}
{% endfor %}
</a>
</body>
</html>