如何使用我从Django中的另一个表单获取的数据填充隐藏字段?

时间:2017-02-27 20:32:38

标签: python django forms

我有一个非常简单的表单form1 [在template1上呈现],text字段为required。点击此表单上的Submit将导致form2,[在template2上呈现]。在template2,我还会显示form1中输入的文字字段中的数据。 form2有两个字段subjectbody,还有一个hidden field字段,用于存储来自form1的文本字段的值。在form2上单击提交后,如何在template3上打印出所有数据(从两种表单中获取)?

这是我到目前为止所做的:

Form1中:

class EmailForm(forms.Form):
    product_code = forms.CharField(required=True, max_length=100, label='Enter Product Code')

窗体2:

class CustomersWhoBoughtForm(forms.Form):
    subject = forms.CharField(required=True, max_length=1000, label='Email Subject')
    body = forms.CharField(widget=forms.Textarea({'rows': 5}),
                           required=True, label=mark_safe('<br>Body'))
    database_product_code = forms.CharField(required=True, widget=forms.HiddenInput())

Views.py

def email_customers(request):
    if request.method == 'POST':
            if form.is_valid():
                # raise Exception(request.POST.get('product_code')) - Executing this line successfully
                if request.POST.get('form_submit') == 'fetch_code':
                    product_code = request.POST.get('product_code')
                    form2 = CustomersWhoBoughtForm(initial={'databse_product_code': product_code})
                    return render(request, 'thanks.html', {'product_code': product_code, 'email_id': 'xyz@ecample.com', 'form2': form2})

                if request.POST.get('form_submit') == 'send_email':
                    form2 = CustomersWhoBoughtForm(request.POST)
                    form2['database_product_code'] = form.cleaned_data['product_code']
                    if form2.is_valid():
                        database_product_code = request.POST.get('database_product_code')
                        email_subject = request.POST.get('subject')
                        email_body = request.POST.get('body')
                        return render(request, 'email_sent.html', {'email_subject': email_subject,
                                                                                'email_body': email_body,
                                                                                'database_product_code': database_product_code,
                                                                                'recipient': 'xyz@example.com'})
                    else:
                        return render(request, 'thanks.html', {'form2': form})
        else:
            form = EmailForm
            return render(request, 'email_customers.html', {'form': form})

    else: #Logic Starts here
        form = EmailForm()
        return render(request, 'email_customers.html', {'form': form})

Template1 - email_customer.html

<h1>Hello</h1>
<p>Email customers page!</p>

<title>Email Customers</title>

<form action="." method="post">
    {{form}}
    {% csrf_token %}
    <button name="form_submit" type="submit" value="fetch_code">Submit</button>
</form>

Template2 - thanks.html

<h3>Thanks for Submitting</h3>
<p>Have a good Day!</p>
<p>You submitted</p>
Product Code: {{product_code}}<br>
Email ID: {{email_id}}
<br><br><br>

<form action="." method="post">
    {{form2}}<br>
    {% csrf_token %}
    <br><button name="form_submit" type="submit" value="send_email">Submit</button>
</form>

Template3 - email_sent.html

Subject: {{email_subject}}
<br><br>
Body: {{email_body}}
<br><br>
Product Code: {{database_product_code}}
<br><br>
Sender: {{recipient}}
<br><br>
Recipient: {{recipient}}

0 个答案:

没有答案