网站首页 > 技术文章 正文
开发环境:
python 3.6django 1.11
场景一
经常有从数据库中获取一批数据,然后在前端以列表的形式展现,比如:获取到所有的用户,然后在用户列表页面展示。
解决方案
常规写法是,我们通过Django的ORM查询到所有的数据,然后展示出来,代码如下:
def user_list(request):
"""返回UserProfile中所有的用户"""
users = UserProfile.objects.all()
return render(request, 'talks/users_list.html', context={"user_list": users})
这样能够解决问题,但是Django针对这种常用场景,提供了一个更快速便捷的方式,那就是ListView,用法如下:
from django.views.generic import ListView
class UsersView(ListView):
model = UserProfile
template_name = 'talks/users_list.html'
context_object_name = 'user_list'
这样我们就完成了上边功能,代码很简洁。
场景二:
我想要对数据做过滤,ListView怎么实现?代码如下:
from django.views.generic import ListView
class UsersView(ListView):
model = UserProfile
template_name = 'talks/users_list.html'
context_object_name = 'user_list'
def get_queryset(self): # 重写get_queryset方法
# 获取所有is_deleted为False的用户,并且以时间倒序返回数据
return UserProfile.objects.filter(is_deleted=False).order_by('-create_time')
如果你要对数据做更多维度的过滤,比如:既要用户是某部门的,还只要获取到性别是男的,这时候,可以使用Django提供的Q函数来实现。
场景三
我想要返回给Template的数据需要多个,不仅仅是user_list,可能还有其他数据,如获取当前登陆用户的详细信息,这时怎么操作?,代码如下:
from django.views.generic import ListView
class UsersView(ListView):
model = UserProfile
template_name = 'talks/users_list.html'
context_object_name = 'user_list'
def get_context_data(self, **kwargs): # 重写get_context_data方法
# 很关键,必须把原方法的结果拿到
context = super().get_context_data(**kwargs)
username = self.request.GET.get('user', None)
context['user'] = UserProfile.objects.get(username=username
return context
这样,你返回给Template页面时,context包含为{'user_list': user_list, 'user': user}。
场景四
我想要限制接口的请求方式,比如限制只能GET访问,代码如下:
from django.views.generic import ListView
class UsersView(ListView):
model = UserProfile
template_name = 'talks/users_list.html'
context_object_name = 'user_list'
http_method_names = ['GET'] # 加上这一行,告知允许那种请求方式
不清楚的地方,可以留言!更多Python教程也会继续更新!
- 上一篇: 小疯谈python:(四)数据类型之序列类型(上)
- 下一篇: 如何做好 Web 自定义排序产品设计?
猜你喜欢
- 2025-07-17 什么是Python列表(python列表有什么用)
- 2025-07-17 我用Cursor+Claude 4 ,44句对话做了个微信小游戏,附全部提示词
- 2025-07-17 深入了解Python列表的高级用法(python列表常用的五种方法)
- 2025-07-17 Python操作Excel库xlrd与xlwt常用操作详解
- 2025-07-17 Python中冷门但非常好用的内置函数
- 2025-07-17 Python教程(9)——Python变量类型列表list的用法介绍
- 2025-07-17 如何做好 Web 自定义排序产品设计?
- 2025-07-17 小疯谈python:(四)数据类型之序列类型(上)
- 2025-07-17 Python range 函数实用指南(range函数的用法python)
- 2025-07-17 Python循环秘技!90%新手不知道的7个for用法,第3个绝了!
- 279℃Python短文,Python中的嵌套条件语句(六)
- 277℃python笔记:for循环嵌套。end=""的作用,图形打印
- 275℃PythonNet:实现Python与.Net代码相互调用!
- 270℃Python实现字符串小写转大写并写入文件
- 269℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 130℃原来2025是完美的平方年,一起探索六种平方的算吧
- 115℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 109℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)