字典更新序列元素#0的长度为6; 2是必需的

时间:2016-03-15 06:35:40

标签: python django forms web

from django.shortcuts import HttpResponse, render
from .validate import create_validate_code
from io import BytesIO
from PIL import Image
from .forms import ContactForm

def test(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)

        if form.is_valid():
            your_name = form.cleaned_data['your_name']
            your_email = form.cleaned_data['your_email']
            your_country = form.cleaned_data['your_country']
            your_Product = form.cleaned_data['your_Product']
            your_comment = form.cleaned_data['your_comment']
            code = form.cleaned_data['code']

            if code == request.session['validate']:
                recipients = ['justman880221@live.com']
                content = your_comment + your_name + your_country

                if your_Product:
                    content.append(your_Product)

                from django.core.mail import send_mail
                send_mail(your_name, content, your_email, recipients)           
                return HttpResponseRedirect('/thanks/')

    else:
        form = ContactForm()

    return render(request, 'Samples/contact.html', {'form', form})

此行导致的错误返回渲染(请求,'Samples / contact.html',{'form',form})

我是Django的新手,有些人可以帮助找出哪个部分出错,以及如何解决这个问题?

  

这是我的form.py

from django import forms


class ContactForm(forms.Form):
    your_name = forms.CharField(label='Your Name', max_length=100)
    your_email = forms.EmailField(label='Your Email')
    your_country = forms.CharField(label='Your Country', max_length=100)
    your_Product = forms.CharField(label='CEEONE Products', required=False, max_length=100)
    your_comment = forms.CharField(label='Comment Or Specific Requests', widget=forms.Textarea, max_length=400)
    code = forms.CharField(label='Type the text', max_length=100)   
  

这是html页面

<form action="/Samples/test/" method="post">
    <p>
    {% csrf_token %}
    {{ form.as_p }}
    </p>
    <p class="form_p">
        <strong class="captcha_box">
            <span class="captcha_img"><img id="refresh_img" src="/Samples/validate/"/> </span> 
            <span onclick="myFunction()" title="Get a new challenge" class="captcha_reload ico" ></span>
        </strong>
    </p>
    <p class="form_p">
        <input class="btn" value="Seed Message" type="submit" />
    </p>
</form>

2 个答案:

答案 0 :(得分:3)

{'form', form}是一个集合文字,而不是字典文字。

应该是(, - &gt; :

{'form': form}

答案 1 :(得分:1)

由于它是字典,因此应为:而不是,

 return render(request, 'Samples/contact.html', {'form': form})