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['sklad_form'] = SkladForm kwargs['pack_form'] = PackForm kwargs['list_sklad'] = Sklad.objects.all().order_by('material') kwargs['list_pack'] = Pack.objects.all().order_by('name_pack') 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() return super().form_valid(form)
class Sklad(models.Model): author = models.ForeignKey(User, on_delete = models.CASCADE, verbose_name='автор склада', null=True) material = models.CharField('название', max_length=200) unit = models.CharField('единица измерения', max_length=200) description = models.CharField('описание', max_length=200, null=True) price_buy = models.IntegerField('цена закупки', ) price_sell = models.IntegerField('цена продажи', ) amount = models.IntegerField('количество', default='0')
def __str__(self): return self.material
class Pack(models.Model): author = models.ForeignKey(User, on_delete = models.CASCADE, verbose_name='автор упаковки', null=True) name_pack = models.CharField('название', max_length=100) price_pack = models.IntegerField('цена', )
def __str__(self): return self.name_pack
class SkladForm(forms.ModelForm): class Meta: model = Sklad fields = ( 'material', 'unit', 'description', 'price_buy', 'price_sell', 'amount', )
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for field in self.fields: self.fields[field].widget.attrs['class'] = 'form-control'
class PackForm(forms.ModelForm): class Meta: model = Pack fields = ( 'name_pack', 'price_pack', )
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) for field in self.fields: self.fields[field].widget.attrs['class'] = 'form-control'
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['sklad_form'] = SkladForm # you do not need this anymore since you have set the form_class above.
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)
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'] |
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):
context = super(SkladCreateView, self).get_context_data(**kwargs) context['pack_form'] = PackForm(self.request.POST) else: context['pack_form'] = PackForm() return super().get_context_data(**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')
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)
you have two returns in get_Context_data. use only this
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'] |
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) 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)
--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b3bcc2cc-aa55-42b1-a322-769f177c1862%40googlegroups.com.
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) 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)
To unsubscribe from this group and all its topics, send an email to django...@googlegroups.com.
To unsubscribe from this group and all its topics, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e6bd5958-e885-488c-85e8-9452589d50cf%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/e6bd5958-e885-488c-85e8-9452589d50cf%40googlegroups.com.
--
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 django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/257e308b-91d3-4fa3-8f8f-ca758b72e7e0%40googlegroups.com.
To unsubscribe from this group and all its topics, send an email to django...@googlegroups.com.