Django初识(3.常用视图之ListView视图)

 五大视图 

动作视图名是否需要Model是否需要Form
CreatView
DeleteView
UpdateView
ListView,DetailView

在网站开发中,经常会出现需要列出某个表中的一些数据作为列表展示出来。比如文章列表,图书列表等等。在Django中可以使用ListView来帮我们快速实现这种需求。

因为要对表中的数据进行操作,那么我们首先的创建一个模型,然后映射到数据库中去。
models.py中写入代码:

#Django01_app02/models.py
from django.db import models


# Create your models here.
class Person(models.Model):
    GENDER_CHOICES = (
        (1, '男'),
        (0, '女')
    )
    name = models.CharField(max_length=32)
    age = models.IntegerField()
    gender = models.BooleanField(choices=GENDER_CHOICES)
    id_card = models.IntegerField()
    address = models.CharField(max_length=255)
    temperature = models.FloatField()

    class Meta:
        permissions = ()

我的models在某个app下面,那么先将app添加至settings.py

'Django01_app02.apps.Django01App02Config'

创建Html文件:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>疫情人员登记表</title>
</head>
<body>
  <table>
    <thead>
      <tr>
        <th>ID</th>
        <th>名字</th>
        <th>年龄</th>
        <th>性别</th>
        <th>疑似</th>
      </tr>
    </thead>
    
    <tbody>
    {% for item in object_list %}
      <tr>
        <td>{{ item.id }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
        <td>{{ item.gender.get_display }}</td>
        <td>{% if item.temperature > 37.4 %}是{% else %}否{% endif %}</td>
      </tr>
    {% empty %}
      <tr>
        <td colspan="5">暂无数据</td>
      </tr>
    {% endfor %}
    </tbody>
  </table>
</body>
</html>

新建一个类视图:

##Django01_app02/views.py
from django.views.generic import ListView
from Django01_app02.models import Person

class PersonList(ListView):
    model = Person
    template_name = 'Django01_app02/personlist.html'

配置urls.py文件:

#Django01_app02/urls.py
from django.urls import path

from Django01_app02.views import PersonList
app_name='Django01_app02'
urlpatterns = [
    path('', PersonList.as_view(),name='peraonlist')
#Django01/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('Django01_app01.urls', namespace='Test')),
    path('personal_info/', include('Django01_app02.urls', namespace='personal'))
]

 我们可以使用默认的SQLite数据库,执行按Alt+Ctrl+R运行manager.py任务执行makenigrations和migrate。将模型映射到数据库中。

映射完成后,运行输入我们设置的网址

http://127.0.0.1:8000/personal_info/

即可得到如下例子

当你需要展示一个对象列表时,可以使用Django内置的ListView视图。以下是一个简单的示例: 1. 首先,在你的Django项目中的views.py文件中导入ListView和你的模型类: ```python from django.views.generic import ListView from .models import YourModel ``` 2. 创建一个继承自ListView视图类,并指定要使用的模型和模板: ```python class YourListView(ListView): model = YourModel template_name = 'your_template.html' ``` 在这个示例中,我们创建了一个名为YourListView视图类,使用YourModel作为数据源,并使用名为your_template.html的模板进行渲染。 3. 在你的urls.py文件中添加一个URL模式来映射到YourListView: ```python from .views import YourListView urlpatterns = [ # 其他URL模式... path('your-list/', YourListView.as_view(), name='your_list'), ] ``` 在这个示例中,我们创建了一个名为"your_list"的URL模式,当用户访问/your-list/时,将调用YourListView来展示对象列表。 4. 创建一个名为"your_template.html"的模板文件,放在你的Django项目中的templates目录下: ```html <!DOCTYPE html> <html> <head> <title>Your Object List</title> </head> <body> <h1>Your Object List</h1> <ul> {% for object in object_list %} <li>{{ object }}</li> {% empty %} <li>No objects found.</li> {% endfor %} </ul> </body> </html> ``` 在这个示例中,我们简单地在模板中添加了一个标题和一个无序列表,通过使用模板标签{% for %}和{{ object }}来遍历并显示对象列表。如果列表为空,将显示"No objects found."。 现在,当用户访问/your-list/时,Django将使用YourListView来获取YourModel的对象列表,并使用your_template.html进行渲染,并将结果返回给用户。 这只是一个简单的示例,你可以根据自己的需求来扩展和定制ListView。可以在Django官方文档中找到更多关于ListView的使用说明和示例代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慵懒之龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值