我试图按照this link中的说明将用户(即request.user)传递给我的模型构造函数。但是我得到了
TypeError: __init__() got an unexpected keyword argument 'us'
错误。这是我的观点
class dealAdd(generic.CreateView):
form_class = dealForm
template_name='deals/deal_Add.html'
def get_context_data(self,**kwargs):
context=super(dealAdd,self).get_context_data(**kwargs)
llist=lead.objects.all()
clist=contacts.objects.all()
context.update({'leadob':llist,'contob':clist})
return context
def get_form_kwargs(self, *args, **kwargs):
kwargs = super(dealAdd, self).get_form_kwargs()
kwargs.update({'us': self.request.user})
return kwargs
和我的表单构造器
def __init__(self,*args,**kwargs):
usr=kwargs.pop('us')
super(dealForm,self).__init__(*args, **kwargs)
print(usr)
# print(self)
回溯说我有错误
context=super(dealAdd,self).get_context_data(**kwargs)
所以我认为有问题吗?
跟踪:
Internal Server Error: /deals/dealAdd
Traceback (most recent call last):
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\core\handlers\base.py", line 128, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\views\generic\base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\views\generic\base.py", line 89, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\views\generic\edit.py", line 168, in get
return super().get(request, *args, **kwargs)
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\views\generic\edit.py", line 133, in get
return self.render_to_response(self.get_context_data())
File "C:\Users\CapC\Desktop\Sabith\crm1\deals\views.py", line 21, in get_context_data
context=super(dealAdd,self).get_context_data(**kwargs)
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\views\generic\edit.py", line 66, in get_context_data
kwargs['form'] = self.get_form()
File "C:\Users\CapC\Desktop\Django\env\lib\site-packages\django\views\generic\edit.py", line 33, in get_form
return form_class(**self.get_form_kwargs())
TypeError: __init__() got an unexpected keyword argument 'us'
[06/Oct/2018 15:18:41] "GET /deals/dealAdd HTTP/1.1" 500 91563
我的表格
class dealForm(forms.ModelForm):
class Meta:
model=deals
fields= '__all__'
widgets={
'refno':forms.TextInput(attrs={'class':'input-sm form-
control ember-view ember-text-field'}),
'status':forms.Select(choices=stat,attrs={'class':'btn btn-deals'}),
'transaction_price':forms.TextInput(attrs={'class':'input-sm form-
control ember-view ember-text-field'}),
'estimated_closing_date':forms.TextInput(attrs={'class':'input-sm
form-control ember-view ember-text-field'}),
答案 0 :(得分:0)
也许是因为订单。
您在get_context_data
之前先进行get_form_kwargs
的CreateView调用。这意味着它会在更新之前“弹出”您的“我们”。
只需更改usr=kwargs.pop('us', None)
即可避免错误,或使用try/except
。