参数不通过表格?

时间:2013-05-31 04:32:16

标签: python django

我正在尝试使用django表格验证演练here

然而,我似乎陷入困境。似乎我的一个参数(收件人)没有通过,即使其他参数是。

这是我的html表单:

<form action= "receiver" method="post" >
{% csrf_token %}
<p><label for="id_subject">Subject:</label>
    <input id="id_subject" type="text" name="subject" maxlength="100" /></p>
<p><label for="id_message">Message:</label>
    <input type="text" name="message" id="id_message" /></p>
<p><label for="id_sender">Sender:</label>
    <input type="text" name="sender" id="id_sender" /></p>
<p><label for="id_recipients">Recipients:</label>
    <input type="text" name="recipients" id="id_recipients" /></p>
<p><label for="id_cc_myself">Cc myself:</label>
    <input type="checkbox" name="cc_myself" id="id_cc_myself" /></p>
<input type="submit" value="Submit" />
</form>

以下是models.py中的相关模型类:

class ContactForm(forms.Form):
    subject = forms.CharField(max_length=100)
    message = forms.CharField()
    sender = forms.EmailField()
    recipients = MultiEmailField(required=True)
    cc_myself = forms.BooleanField(required=False)

以下是相关视图:

def reciever(request):
    form = ContactForm(request.POST) # A form bound to the POST data  
    print form
    if request.method == 'POST':
        if form.is_valid():
            subject = form.cleaned_data['subject']
            message = form.cleaned_data['message']
            sender = form.cleaned_data['sender']
            cc_myself = form.cleaned_data['cc_myself']      
            recipients = form.cleaned_data['recipients']
            template = loader.get_template('helloworld/receiver.html')

            context = RequestContext(request,{
                    'subject':subject,                          
                    'sender':sender,
                    'message': message,
                    'cc_myself': cc_myself,
                    'recipients': recipients,
        })
        return HttpResponse(template.render(context))
    else:
        context = RequestContext(request,{
            'form': form, 
        })
        template = loader.get_template('helloworld/wrong.html')
        return HttpResponse(template.render(context))

我不太了解视图,我可以从我的打印表单中看到请求无效,输出如下:

<tr><th><label for="id_subject">Subject:</label></th><td><input id="id_subject" maxlength="100" name="subject" type="text" value="slkj" /></td></tr>
<tr><th><label for="id_message">Message:</label></th><td><input id="id_message" name="message" type="text" value="lkjqw" /></td></tr>
<tr><th><label for="id_sender">Sender:</label></th><td><input id="id_sender" name="sender" type="text" value="ij@aol.com" /></td></tr>
**<tr><th><label for="id_recipients">Recipients:</label></th><td><ul class="errorlist"><li>This field is required.</li></ul><input id="id_recipients" name="recipients" type="text" /></td></tr>**
<tr><th><label for="id_cc_myself">Cc myself:</label></th><td><input id="id_cc_myself" name="cc_myself" type="checkbox" /></td></tr>

所以出于某种原因,即使我在表单框中输入信息(fake@aol.com),它似乎也没有通过表单,我不太清楚为什么。

编辑:对不起,我忘记了一个重要的部分。我还根据教程的说明在model.py中添加了一个自定义类:

class MultiEmailField(forms.Field):
    def to_python(self, value):
        "Normalize data to a list of strings."

        # Return an empty list if no input was given.
        if not value:
            return []
        return value.split(',')

    def validate(self, value):
        "Check if value consists only of valid emails."

        # Use the parent's handling of required fields, etc.
        super(MultiEmailField, self).validate(value)

        for email in value:
            validate_email(email)

0 个答案:

没有答案