【问题标题】:dictionary update sequence element #0 has length 6; 2 is required字典更新序列元素#0的长度为6; 2 是必需的
【发布时间】:2016-06-30 11:26:06
【问题描述】:
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})

这行导致的错误return render(request, '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>

【问题讨论】:

    标签: python django forms web


    【解决方案1】:

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

    应该是(, --> :

    {'form': form}
    

    【讨论】:

      【解决方案2】:

      既然是字典,应该是:而不是,

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-06-27
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 2018-10-22
        • 2015-02-22
        • 2019-08-10
        相关资源
        最近更新 更多