0

我有一个未将订购的项目保存到数据库的订单。我只保存了联系表格。我已经搜索过,但在我的观点中找不到我做错了什么或遗漏了什么。任何帮助将不胜感激。谢谢!

意见:

class OrderFormView(CreateView):
    model = Contact
    form_class = ContactForm
    template_name = 'orderform.html'
    success_url = 'thank-you'

    def get_context_data(self, **kwargs):
        context = super(OrderFormView, self).get_context_data(**kwargs)
        formsets = dict(
            letterhead_formset=LetterHeadFormSet(prefix='letterhead'),
            windowenv_formset=WindowEnvFormSet(prefix='windowenv'),
            numbertenenv_formset=NumberTenEnvFormSet(prefix='numbertenenv'),
            ninebytwelveenv_formset=NineByTwelveEnvFormSet(prefix='ninebytwelveenv'),
            tenbythirteenenv_formset=TenByThirteenEnvFormSet(prefix='tenbythirteenenv'),
            businesscard_formset=BusinessCardFormSet(prefix='businesscard'),
        )
        context.update({
            'letterhead_form': LetterHeadForm,
            'windowenv_form': WindowEnvForm,
            'numbertenenv_form': NumberTenEnvForm,
            'ninebytwelveenv_form': NineByTwelveEnvForm,
            'tenbythirteenenv_form': TenByThirteenEnvForm,
            'businesscard_form': BusinessCardForm,
            'rushorder_form': RushOrderForm,
            'ordernote_form': OrderNoteForm,
            })
        context.update(formsets.items())
        return context

    def form_valid(self, form):
        if form.is_valid():
            data = form.cleaned_data
            email = OrderFormNotification(to=[settings.NO_REPLY_EMAIL_ADDRESS, ],
                extra_context=data)
            email.send()
            form.save()


        return super(OrderFormView, self).form_valid(form)

表单类:

class ContactForm(forms.ModelForm):
    address = forms.ChoiceField(required = True, widget=RadioSelect(),     choices=ADDRESS_CHOICES,)
    class Meta:
         model = Contact


class LetterHeadForm(forms.ModelForm):
    address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES)
    class Meta:
        model = LetterHead
        widgets = {
            'contact': forms.HiddenInput,
            'quantity': forms.Select(choices=QUANTITY_CHOICES),
        }


class WindowEnvForm(forms.ModelForm):
    address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES)
    class Meta:
        model = WindowEnv
        widgets = {
            'contact': forms.HiddenInput,
            'quantity': forms.Select(choices=QUANTITY_CHOICES),
        }


class NumberTenEnvForm(forms.ModelForm):
    address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES)
    class Meta:
        model = NumberTenEnv
        widgets = {
            'contact': forms.HiddenInput,
            'quantity': forms.Select(choices=QUANTITY_CHOICES),
        }


class NineByTwelveEnvForm(forms.ModelForm):
    address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES)
    class Meta:
        model = NineByTwelveEnv
        widgets = {
            'contact': forms.HiddenInput,
            'quantity': forms.Select(choices=QUANTITY_CHOICES),
        }


class TenByThirteenEnvForm(forms.ModelForm):
    address = forms.ChoiceField(required = False, widget=RadioSelect(), choices=ADDRESS_CHOICES)
    class Meta:
        model = TenByThirteenEnv
        widgets = {
            'contact': forms.HiddenInput,
            'quantity': forms.Select(choices=QUANTITY_CHOICES),
        }


class BusinessCardForm(forms.ModelForm):
    print_choices = forms.ChoiceField(required = True, widget=RadioSelect(), choices=PRINT_CHOICES)
    card_styles = forms.ChoiceField(required = True, widget=RadioSelect(), choices=CARD_CHOICES)
    card_mailing_address = forms.ChoiceField(required = True, widget=RadioSelect(), choices=ADDRESS_CHOICES)
    class Meta:
        model = BusinessCard
        widgets = {
            'contact': forms.HiddenInput,
        }


class RushOrderForm(forms.ModelForm):
    class Meta:
        model = RushOrder
        widgets = {
            'contact': forms.HiddenInput,
            'rush_order': forms.CheckboxInput,
            'in_hand_date': forms.extras.SelectDateWidget
        }


class OrderNoteForm(forms.ModelForm):
    class Meta:
        model = OrderNote
        widgets = {
            'contact': forms.HiddenInput,
            'add_note': forms.CheckboxInput,
            'notes': forms.Textarea
        }


LetterHeadFormSet = modelformset_factory(LetterHead,
    form=LetterHeadForm, extra=2, max_num=2)
WindowEnvFormSet = modelformset_factory(WindowEnv,
    form=WindowEnvForm, extra=2, max_num=2)
NumberTenEnvFormSet = modelformset_factory(NumberTenEnv,
    form=NumberTenEnvForm, extra=2, max_num=2)
NineByTwelveEnvFormSet = modelformset_factory(NineByTwelveEnv,
    form=NineByTwelveEnvForm, extra=2, max_num=2)
TenByThirteenEnvFormSet = modelformset_factory(TenByThirteenEnv,
    form=TenByThirteenEnvForm, extra=2, max_num=2)
BusinessCardFormSet = modelformset_factory(BusinessCard,
    form=BusinessCardForm, extra=2, max_num=2)

楷模:

PRINT_CHOICES = (
    ('exact reprint', 'Exact Reprint'),
    ('reprint with changes', 'Reprint With Changes'),
    ('new', 'New')
)

QUANTITY_CHOICES = (
    ('1000', '1000'),
    ('2500', '2500'),
    ('5000', '5000')
)

CARD_QUANTITY_CHOICES = (
    ('250', '250'),
    ('500', '500'),
    ('1000', '1000')
)

CARD_CHOICES = (
    ('chef/black', 'Chef/Black'),
    ('executive/red', 'Executive/Red')
)

ADDRESS_CHOICES = (
    ('eisenhower', 'Eisenhower'),
    ('wheeler', 'Wheeler'),
)


class Contact(models.Model):
    first_name = models.CharField(max_length=100, help_text="First Name")
    last_name = models.CharField(max_length=100, help_text="Last Name")
    email_address = models.EmailField(max_length=275)
    address = models.CharField(max_length=10, choices=ADDRESS_CHOICES)

    def __unicode__(self):
        return "%s %s" % (self.first_name, self.last_name)


class BaseStationary(models.Model):
    contact = models.ForeignKey(Contact, related_name='%(class)s_related')
    address = models.CharField(max_length=10, choices=ADDRESS_CHOICES)
    quantity = models.CharField(max_length=4, choices=QUANTITY_CHOICES)

    class Meta:
        abstract = True


class LetterHead(BaseStationary):
    pass


class WindowEnv(BaseStationary):
    pass


class NumberTenEnv(BaseStationary):
    pass


class NineByTwelveEnv(BaseStationary):
    pass


class TenByThirteenEnv(BaseStationary):
    pass


class BusinessCard(models.Model):
    contact = models.ForeignKey(Contact, related_name='businesscards')
    card_first_name = models.CharField(max_length=100)
    card_last_name = models.CharField(max_length=100)
    title = models.CharField(max_length=100)
    print_choices = models.CharField(max_length=19, choices=PRINT_CHOICES)
    card_styles = models.CharField(max_length=12, choices=CARD_CHOICES)
    card_email_address = models.EmailField(max_length=275)
    office_phone_number = PhoneNumberField(_('main office phone number'),
         blank=True, null=True)
    toll_free_number = PhoneNumberField(_('toll free number'),
    blank=True, null=True)
    mobile_number = PhoneNumberField(_('mobile phone number'),
        blank=True, null=True)
    fax_number = PhoneNumberField(_('main office fax'),
         blank=True, null=True)
    card_mailing_address = models.CharField(max_length=10,
        choices=ADDRESS_CHOICES)
    card_quantity = models.CharField(max_length=3,
    choices=CARD_QUANTITY_CHOICES)


class RushOrder(models.Model):
    contact = models.ForeignKey(Contact, related_name='rushorders')
    rush_order = models.BooleanField()
    in_hand_date = models.DateField(blank=True, null=True)


class OrderNote(models.Model):
    contact = models.ForeignKey(Contact, related_name='ordernotes')
    add_note = models.BooleanField()
    notes = models.TextField()
4

1 回答 1

0

form_valid并且您添加到其中的代码仅处理form_class. 您必须手动验证并保存每个表单集。

于 2012-08-01T20:21:17.483 回答