一、首页的视图函数
from django.shortcuts import render
from commodity.models import *
def indexView(request):
title = '首页'
classContent = ''
commodityInfos = CommodityInfos.objects.order_by('-sold').all()[:8]
types = Types.objects.all()
# 宝宝服饰
cl = [x.seconds for x in types if x.firsts == '儿童服饰']
clothes = CommodityInfos.objects.filter(types__in=cl).order_by('-sold')[:5]
# 奶粉辅食
fl = [x.seconds for x in types if x.firsts == '奶粉辅食']
food = CommodityInfos.objects.filter(types__in=fl).order_by('-sold')[:5]
# 宝宝用品
gl = [x.seconds for x in types if x.firsts == '儿童用品']
goods = CommodityInfos.objects.filter(types__in=gl).order_by('-sold')[:5]
return render(request, 'index.html', locals())
二、使用视图类实现商城首页
在项目应用 index
的 urls.py
重新定义路由 index
:
from django.urls import path
from .views import *
urlpatterns = [
# path('', indexView, name='index'),
path('', indexClassView.as_view(), name='index'),
]
我们将路由 index
改为视图类 indexClassView
,如果路由的视图为视图类,数量图雷必须由调用函数方法 as_view()
,这是对视图类进行实例化处理。下一步在项目应用 index
的 views.py
定义视图类 indexClassView
:
from django.views.generic.base import TemplateView
from commodity.models import *
class indexClassView(TemplateView):
template_name = 'index.html'
template_engine = None
content_type = None
extra_context = {'title': '首页', 'classContent': ''}
# 重新定义模板上下文的获取方式
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['commodityInfos'] = CommodityInfos.objects.order_by('-sold').all()[:8]
types = Types.objects.all()
# 宝宝服饰
cl = [x.seconds for x in types if x.firsts == '儿童服饰']
context['clothes'] = CommodityInfos.objects.filter(types__in=cl).order_by('-sold')[:5]
# 奶粉辅食
fl = [x.seconds for x in types if x.firsts == '奶粉辅食']
context['food'] = CommodityInfos.objects.filter(types__in=fl).order_by('-sold')[:5]
# 宝宝用品
gl = [x.seconds for x in types if x.firsts == '儿童用品']
context['goods'] = CommodityInfos.objects.filter(types__in=gl).order_by('-sold')[:5]
return context
# 定义HTTP的GET请求处理方法
# 参数request代表HTTP请求信息
# 若路由设有路由变量,则可从参数kwargs里获取
def get(self, request, *args, **kwargs):
pass
context = self.get_context_data(**kwargs)
return self.render_to_response(context)
# 定义HTTP的POST请求处理方法
# 参数request代表HTTP请求信息
# 若路由设有路由变量,则可从参数kwargs里获取
def post(self, request, *args, **kwargs):
pass
context = self.get_context_data(**kwargs)
return self.render_to_response(context)