[image: kwargs.JPG]
But the form is empty.
class SkladCreateView(LoginRequiredMixin, CustomSuccessMessageMixin,
CreateView):
model = Sklad
template_name = 'sklad.html'
form_class = SkladForm
success_url = reverse_lazy('sklad')
success_msg = 'Материал сохранён'
def get_context_data(self, **kwargs):
kwargs['list_sklad'] = Sklad.objects.all().order_by('material')
kwargs['list_pack'] = Pack.objects.all().order_by('name_pack')
kwargs['list_hole'] = Hole.objects.all().order_by('name_hole')
context = super(SkladCreateView, self).get_context_data(**kwargs)
if self.request.POST:
context['pack_form'] = PackForm(self.request.POST)
else:
context['pack_form'] = PackForm()
return super().get_context_data(**kwargs)
def form_valid(self, form):
self.object = form.save(commit=False)
self.object.author = self.request.user
self.object.save()
context = self.get_context_data()
pack_form = context['pack_form']
# if pack_form.is_valid():
# pack_form.instance = self.object
# pack_form.save()
print(pack_form)
return super().form_valid(form)
вторник, 5 мая 2020 г., 22:17:08 UTC+2 пользователь Sherif Adigun написал:
>
> Please remove
>
> if pack_form.is_valid():
> pack_form.instance = self.object
> pack_form.save()
>
>
> And replace with print(pack_form)
>
>
> Then let's see what it's printing in the console
>
> On Tue, May 5, 2020, 8:30 PM Sergei Sokov <[email protected] <javascript:>>
> wrote:
>
>> I have this error when I try save data for SkladForm, when I press submit
>> Request Method: POST
>> Request URL: http://192.168.0.249:8000/sklad
>> Django Version: 3.0.5
>> Exception Type: KeyError
>> Exception Value:
>>
>> 'pack_form'
>>
>> Exception Location: /var/workspace/myp4/webprint/print/views.py in
>> form_valid, line 362
>> line 362 is pack_form = context['pack_form']
>>
>> And the PackForm is empty, when I call it.
>>
>>
>> class SkladCreateView(LoginRequiredMixin, CustomSuccessMessageMixin,
>> CreateView):
>> model = Sklad
>> template_name = 'sklad.html'
>> form_class = SkladForm
>> success_url = reverse_lazy('sklad')
>> success_msg = 'Материал сохранён'
>> def get_context_data(self, **kwargs):
>> kwargs['list_sklad'] = Sklad.objects.all().order_by('material')
>> kwargs['list_pack'] = Pack.objects.all().order_by('name_pack')
>> kwargs['list_hole'] = Hole.objects.all().order_by('name_hole')
>> context = super(SkladCreateView, self).get_context_data(**kwargs)
>> if self.request.POST:
>> context['pack_form'] = PackForm(self.request.POST)
>> else:
>> context['pack_form'] = PackForm()
>> return super().get_context_data(**kwargs)
>>
>> def form_valid(self, form):
>> self.object = form.save(commit=False)
>> self.object.author = self.request.user
>> self.object.save()
>>
>> context = self.get_context_data()
>> pack_form = context['pack_form']
>> if pack_form.is_valid():
>> pack_form.instance = self.object
>> pack_form.save()
>> return super().form_valid(form)
>>
>>
>>
>>
>> вторник, 5 мая 2020 г., 18:08:56 UTC+2 пользователь Sherif Adigun написал:
>>>
>>> you have two returns in get_Context_data. use only this
>>>>
>>>
>>>
>>> def get_context_data(self, **kwargs):
>>> context = super(SkladCreateView, self).get_context_data(**kwargs)
>>> if self.request.POST:
>>> context['pack_form'] = PackForm(self.request.POST)
>>> else:
>>> context['pack_form'] = PackForm()
>>> return super().get_context_data(**kwargs)
>>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit
>> https://groups.google.com/d/topic/django-users/I_32m8lWd-g/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> [email protected] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-users/b3bcc2cc-aa55-42b1-a322-769f177c1862%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/django-users/b3bcc2cc-aa55-42b1-a322-769f177c1862%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/e6bd5958-e885-488c-85e8-9452589d50cf%40googlegroups.com.