Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 不了解表单集的Noobie_Django_Django Forms - Fatal编程技术网

Django 不了解表单集的Noobie

Django 不了解表单集的Noobie,django,django-forms,Django,Django Forms,我有一个实例,我正在创建一个具有多个模型的订单。我希望这些模型以一种形式呈现,并被告知形式集是我的答案。我一直在研究它是如何工作的,但我仍然不知道它是如何运转的。对不起,如果这是简单的,我没有看到它 以下是我的模型: class Contact(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) email_add

我有一个实例,我正在创建一个具有多个模型的订单。我希望这些模型以一种形式呈现,并被告知形式集是我的答案。我一直在研究它是如何工作的,但我仍然不知道它是如何运转的。对不起,如果这是简单的,我没有看到它

以下是我的模型:

class Contact(models.Model):
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    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=3, 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()
这是我的表格:

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 = True, widget=RadioSelect(attrs={'id': 'letterhead_address', 'required': 'True'}), choices=ADDRESS_CHOICES)
    class Meta:
        model = LetterHead
        widgets = {
            'contact': forms.HiddenInput,
            'quantity': forms.Select(attrs={'id': 'letterhead_quantity'}, choices=QUANTITY_CHOICES),
        }


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


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


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


class TenByThirteenEnvForm(forms.ModelForm):
     address = forms.ChoiceField(required = True, widget=RadioSelect(attrs={'id': 'tenbythirteenenv_address', 'required': 'True'}), choices=ADDRESS_CHOICES)
    class Meta:
        model = TenByThirteenEnv
        widgets = {
            'contact': forms.HiddenInput,
            'quantity': forms.Select(attrs={'id': 'tenbythirteenenv_quantity'}, 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
        }
以下是我的观点:

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

def get_context_data(self, **kwargs):
    context = super(OrderFormView, self).get_context_data(**kwargs)
    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,
        })
    return context

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

提前感谢您提供的任何见解。即使这是为了给我指明更好地理解表单集的方向

如果你需要9种不同型号的9张表格,那么我认为表格集对你没有帮助。表单集用于构造同一类型的多个表单。同样地,
CreateView
仅用于创建单个模型的简单情况。如果您正在创建多个模型/验证多个表单,您将发现自己正在使用
CreateView
来实现这一点。您最好编写自己的视图类,该类是从
ProcessFormView
构建的,甚至可能是
view

@MarkLavin我需要将这些模型表单制作成模板中的一个表单…而不是9个表单。我被告知要使用表单集,但从我读到的所有内容来看,我不明白如何用9个模型表单实现它们。谢谢@MarkLavin,我将研究另外两个基于类的视图。当我被告知表单集是我所需要的时,从我所读到的内容来看,这毫无意义。