首页 文章

错误/ contact / - [Errno 111]连接被拒绝(在djangobook中,第7章:表格)

提问于
浏览
3

当我从djangobook(第7章 - 表格)中的例子中学习django时,我得到了这个错误 .

完整错误:错误/ contact /

[Errno 111]连接被拒绝

请求方法:POST请求URL:http://localhost:8000/contact/ Django版本:1.8.4异常类型:错误异常值:

[Errno 111]连接被拒绝

异常位置:/usr/lib/python2.7/socket.py在create_connection中,第571行Python可执行文件:/ usr / bin / python Python版本:2.7.6

这是代码:

views.py

def contact(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            cd = form.cleaned_data
            send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email', 'noreply@example.com'),
                ['siteowner@example.com'],
            )
            return HttpResponseRedirect('/contact/thanks/')
    else:
        form = ContactForm()
    return render(request, 'contact_form.html', {'form': form})

请解决此错误,非常感谢

1 回答

  • 3

    看起来你正在尝试发送没有运行电子邮件服务器的电子邮件,或者没有配置Django指向它 .

    如果您只是在本地测试,Django documentation有一些建议 .


    你可以跑

    python -m smtpd -n -c DebuggingServer localhost:1025
    

    然后将Django的EMAIL_HOST设置配置为“localhost”,将EMAIL_PORT设置为“1025”

    # in your settings.py
    EMAIL_HOST = "localhost"
    EMAIL_PORT = 1025
    

    或者将您的EMAIL_BACKEND设置为

    # in your settings.py
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
    

    查看生成的电子邮件输出到您的控制台 .

相关问题