Proyecto Django (3)

Consulta de la página de detalles de la organización

class OrgView(View):
	def get(self,request):
		# 通过model 从数据库种擦查询 课程 + 城市
		list_orgs = CoureseOrg.objects.all()
		list_citys = City.objects.all()
		
		# 获取客户端提交数据(有就获取,没有就为空)
		category = request.GET.get('ct','')
		
		if category :
			# 数据根据 category 进行筛选数据
			list_orgs = list_orgs.filter(catagory = category)
		
		# 获取用户提交上来的城市数据(有就获取,没有就为空)
		city_id = request.GET.get('city','')
		if city_id:
			list_orgs = list_orgs.filter(city = ciyt_id)	
		
		# 对机构进行排序(按照学生人数、或课程数)
		sort = request.GET.get('sort','')
		if sort == 'sutdents':
			list_orgs = list_orgs.order_by('-studengs')
		elif sort == 'courses':
			list_orgs = list_orgs.order_by('-course_nums')
			
		# 计算一共有多少条数据
		org_nums = list_orgs.count()
		try:
			page = request.GET.get('page',1)
		except PageNotAnInteger:
			page = 1
			
		# 对机构进行分页
		p = Paginator(list_orgs,per_page=1,request = request)
		
		orgs = p.page(page)

		# 数据返回
		return render(rquest,'org-list.html',{
				'list_orgs':orgs,
				'list_citys':list_citys,
				'city_id':city_id,
				'category':category,
				'org_nums':org_nums,
				'sort':sort,
				})

# 前端模板选中状态(如果sort =='',则标记为选中)
<li class = "{% if sort == ''%} active {% endif %}"><a></li>

Consulta inversa
de datos Hay un bucle en los datos de la consulta, y también hay datos en bucle en el bucle,
por ejemplo: consulta instituciones + cursos en instituciones

# model中 —— 相应的 模型 class 类下
def course(self):
	from apps.courses.models import Course
	# 方法1
	coureses = Course.objects.filter(course_org = self)
	# 方法2(最好用方法2)
	courses = self.course_set.filter(is_classics = True)[0:3]
	return courses

Paginación institucional

# 安装分页的库
pip install django-pure-pagination
# 添加到INSTALLED_APPS中
INSTALLED_APPS = (
	'pure_pagination')
# settings中进行设置
PAGINATION_SETTINGS = {
	# 页面中展示数量 10个
	'PAGE_RANGE_DISPLAYED':10,
	# 分页标签前后展示2个
	'MARGIN_PAGES_SISPLAYED':2,
	'SHOW_FIRST_PAGE_WHEN-INVALID': True,
	}

# views中导入
from django.shortcuts import render_to_response
from pur_pagination import Paginator,EmptyPage,PageNotAnteger

50 artículos originales publicados · Me gusta3 · Visitas 1797

Supongo que te gusta

Origin blog.csdn.net/weixin_43056654/article/details/105049634
Recomendado
Clasificación