通用视图基础

使用通用视图的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>

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值