Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
230 views
in Technique[技术] by (71.8m points)

python - AttributeError: 'tuple' object has no attribute 'encode' (django contact form)

I am creating a Contact page for my django project. forms.py has name, subject, sender and message. Here's the view:

def contact(request):
if request.method == 'GET':
    form = ContactForm()
else:
    form = ContactForm(request.POST)
    if form.is_valid():
        name = form.cleaned_data['name']
        sender_email = form.cleaned_data['sender_email']
        subject = form.cleaned_data['subject']
        message = form.cleaned_data['message']

        send_mail(subject, message, sender_email, ['***@gmail.com'])
return render(request, 'contact_us.html', {'form': form})

The template:

<h1>Contact Us</h1>
<form method="POST">
    {%csrf_token%}
    {{form.as_p}}
    <div class="form-actions">
        <button type="submit">Send</button>
    </div>
</form>

After I click Send I get this error: AttributeError: 'tuple' object has no attribute 'encode' and I can't seem to figure out what's wrong despite checking other questions of the same type the last couple of days. I have followed instructions from link 1 and link 2 to set up the SMTP server (2 different cases) and it gives me the same error. How can I fix that? Thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If anyone ever faces the same issue, check your settings.py file. It might have trailing commas. This was the issue for me.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...