1、view和url传递参数
参数方式一: url.py path('xxx/', MailTemplateList.as_view(), name='MailTemplateList'), path('xxx/<str:id>/', MailTemplateList.as_view(), name='MailTemplateList'), 说明: http://xxx/id 这个id可以在 get(self,request,id=none) 方法中,用实参数获取,必须同名,也叫id 参数方式二:html的href <a href="xxx/?{%p_code=130000%}">{{ 河北省 }} p_code = kwargs.get('p_code') #如上,p_code的获取方法是kwargs字典
2、Form传递参数
#(1)view中调用Form formset = KhPersonResultFormSet(username=request.user.username, period_id=self.kwargs.get('period_id')) #(2)form的init中,提取参数,进行动态初始化 class KhPersonResultFormSet(RecordBase): def __init__(self, *args, **kwargs): self.username = kwargs.pop('username') self.period_id = kwargs.pop('period_id') super().__init__(*args, **kwargs) # myuser = get_object_or_404(User, username=self.username) self.queryset = KhPersonResult.objects.filter(zhukao_user=self.username, kaohe_period=self.period_id).order_by('beikao_user__user_type','beikao_user__jg__id','beikao_user__p_bh')
标签:__,username,self,period,django,传递,参数,kwargs,id From: https://www.cnblogs.com/lxgbky/p/17546701.html