django如何渲染2个类在模板中形成表单

时间:2015-09-02 09:29:50

标签: python django django-forms django-templates

我正在尝试在表单中呈现2个类并将其显示在另一个模板中,我不知道该做什么,并且将非常感谢帮助

这是我在forms.py中的代码

class sample_tableform(ModelForm):

name = forms.CharField( max_length=50, label = ("name"),  help_text = ("Name:"),
    error_messages = {'invalid':"Your name may only contain letters.", 'required':"Enter your name.", 'unique':'Name already exists'}, validators=[RegexValidator( regex='^[a-zA-Z ]*$', message=("Forgotten message."),)])
email = forms.CharField( max_length=50, label = ("email"), help_text = ("Email:"),
    error_messages = {'invalid':"Enter a valid email.", 'required':"Enter your email.", 'unique':'email already exists'}, validators=[RegexValidator( regex='^[\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4} *$', message=("Forgotten message."),)])
pnum = forms.CharField( max_length=11, min_length=11, label = ("pnum"), help_text = ("Phone Number:"),
    error_messages = {'invalid':"Enter a valid phone number.", 'required':"Enter your phone number.", 'min_length':"Should contain 11 digits"}, validators=[RegexValidator( regex='^[0-9]*$', message=("Forgotten message."),)])
class Meta:
    model = sample_table
    fields = ('name', 'email', 'pnum',)

类确认(ModelForm):

pnum = forms.CharField( max_length=11, min_length=11, label = ("pnum"), help_text = ("Phone Number:"),

class Meta:
    model = sample_table
    fields = ('confirm',)`

提前谢谢你:*

1 个答案:

答案 0 :(得分:0)

在视图中,您可以通过上下文传递两个表单对象。 我想你可以这样做:

context = Context({'form_1': Form_1, 'form_2': Form_2})

在此处查看有关stackoverflow的类似先前答案:https://stackoverflow.com/a/15499249/3967218:)