django 项目 处理分页返回的问题、跟进和报名
分页的问题:
处理思路:
获取到跳转前的页面的url和url后的数据
url = request.path_info
param = request.GET.copy()
装入到querydict中:
qd = QueryDict()
qd._mutable = True
字典进行赋值:
qd['next'] = url
qd['_query'] = param.urlencode()
转化为&连接的方式
query = qd.urlencode()
进行跳转:1,增加页面,url后的内容
add_btn = mark_safe(
'<a href="{0}?{1}" class="btn btn-sm btn-primary">增加</a>'.format(reverse('add_client'),query)
)
返回:add_btn query
返回给查看的页面:
add_btn,query = self.get_btn_add(request)
'add_btn':add_btn,
'query':query,
在跳转的页面获取到
add_btn query
进行跳转:
return redirect('{}?{}'.format(next_url,query))
跟进:
生成一个ConsultRecord对象,没有放在数据库
obj = models.ConsultRecord(consultant=request.user)
在做字段筛选的时候
class ConsultForm(BaseForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['customer'].widget.choices = [(i.id,i) for i in self.instance.consultant.customers.all()]
self.fields['consultant'].widget.choices = [(self.instance.consultant.id,self.instance.consultant)]
class Meta:
model = models.ConsultRecord
fields = '__all__'
exclude = ['delete_status']
在init 下进行字段的筛选,进行form时选择字段使用choices方法进行筛选
修改时的方式:
customer_obj = enroll_obj.customer
customer_obj.status = 'signed'
customer_obj.save()
函数的复用和选择:
class ConsultRecordList(View):
def get(self,request,customer_id):
if customer_id == '0': 这里执行判断来区分是所有和个人的
all_consult_records = models.ConsultRecord.objects.filter(consultant=request.user) 筛选user的
else:
all_consult_records = models.ConsultRecord.objects.filter(consultant=request.user,customer_id = customer_id) 筛选user的和指定的id
return render(request,'consult_record_list.html',{'all_consult_records':all_consult_records})
def consult_record(request,edit_id = None):
obj = models.ConsultRecord.objects.filter(id=edit_id).first() or models.ConsultRecord(consultant=request.user) or的执行前者为None执行后边的
form_obj = ConsultForm(instance=obj)
if request.method == 'POST':
form_obj = ConsultForm(request.POST,instance=obj)
if form_obj.is_valid():
form_obj.save()
return redirect(reverse('consult_record_list'))
return render(request, 'consult_record.html', {"form_obj": form_obj})
在modes中自定义进行判断:
def enroll_link(self):
if self.enrollment_set.all(): 用反向查找来进行
return format_html('<a href="{}">查看报名表</a>'.format(reverse('enrollment_list', args=(self.id,))))
else:
return format_html('<a href="{}">报名地址</a>'.format(reverse('add_enrollment', args=(self.id,))))