Please join the meeting now let's quickly fix it .

https://us04web.zoom.us/j/77729655336?pwd=c1o2Q0JQOFJiYm5hTlJTYjgra0dOQT09

On Monday, May 4, 2020 at 4:37:39 PM UTC+1, Sergei Sokov wrote:
>
> I have two forms in one template, but one of them doesn't  have data to my 
> database.
> Why does it happen and how to fix it?
>
> When I fill out the Pack form and press "submit" the terminal shows that: 
> "POST" /sklad HTTP/1.1" 200 9937
> This data doesn't save to my database.
>
> When I fill out the Sklad form and press "submit" the terminal shows that: 
> "POST" /sklad HTTP/1.1" 302 0
> This data saves to my database fine.
>
> views.py
> 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)
>
> models.py
> 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
>
>
> forms.py
> 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'
>
>
>

-- 
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 django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/dd44ad0a-7059-4226-9200-ebb24fcf6b1d%40googlegroups.com.

Reply via email to