Я хотел бы использовать kwargs
и передать элемент kwargs из Django CBV
в мой файл формы в __init__
.
У меня есть View class
с get_context_data()
, который позволяет подобрать ввод по электронной почте, заполненный пользователем:
class HomeView(FormView):
form_class = CustomerForm
def get_context_data(self, **kwargs):
if "DocumentSelected" in self.request.GET:
customer_email = self.request.GET['EmailDownloadDocument']
kwargs['customer_email'] = customer_email
return super(HomeView, self).get_context_data(**kwargs)
И у меня есть файл forms.py с этой частью
class CustomerForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
customer_email = kwargs.pop('customer_email', None)
super(CustomerForm, self).__init__(*args, **kwargs)
if customer_email is not None:
self.fields['email'].initial = customer_email
self.fields['first_name'].initial = Customer.objects.get(email__iexact=customer_email).first_name
self.fields['last_name'].initial = Customer.objects.get(email__iexact=customer_email).last_name
self.fields['country'].initial = Customer.objects.get(email__iexact=customer_email).country_id
self.fields['institution'].initial = Customer.objects.get(email__iexact=customer_email).institution
class Meta:
model = Customer
fields = ['email', 'first_name', 'last_name', 'country', 'institution']
widgets = {
'email': forms.TextInput(attrs={'placeholder': _('name@example.com')}),
'first_name': forms.TextInput(attrs={'placeholder': _('First Name')}),
'last_name': forms.TextInput(attrs={'placeholder': _('Last Name')}),
'institution': forms.TextInput(attrs={'placeholder': _('Agency, company, academic or other affiliation')}),
}
Однако он возвращает None
в моем файле формы, в то время как мой get_context_data()
печатает адрес электронной почты.
Что-то не так в этой части?