编辑:如果你想测试我已经将代码部署到这个github repo
我要实现的基本功能是从表单发送邮件。几乎与您网站上的任何其他“联系我们”表格一样。演示网址是 takemailer.appspot.com
可以在 url 中看到的表单向服务器发送一个 post 请求。我处理请求的视图如下:
def post_data(request):
logging.info(request.POST)
frm_name = request.POST['name'] frm_mail = request.POST['email']
frm = frm_name + " <" + frm_mail + ">"
frm = '"%s"' % frm #above two lines # done to produce a format like "Name <name@mail.com>"
sub = request.POST['subject']
cmnt = request.POST['comment']
extra = str(frm + sub + cmnt)
logging.info(frm)
a = mail.send_mail(sender=frm,
to="Albert Johnson <du***@gmail.com>",
subject=sub,
body=cmnt)
logging.info(a)
return http.HttpResponse("1")
上述版本的代码不起作用并引发
<class 'django.core.exceptions.ImproperlyConfigured'>: You haven't set the DATABASE_ENGINE setting yet.
Stacktrace 附在底部。
但是,如果我修改视图函数以硬编码来自电子邮件的 a,如下所示,它可以无缝工作:
def post_data(request):
logging.info(request.POST)
sub = request.POST['subject']
cmnt = request.POST['comment']
a = mail.send_mail(sender="Albert Johnson <du***@gmail.com>",
to="Albert Johnson <du***@gmail.com>",
subject=sub,
body=cmnt)
logging.info(a)
return http.HttpResponse("1")
知道为什么上面的方法有效,而上面的方法无效,并引发这样的错误吗?
堆栈跟踪:
Traceback (most recent call last):
File "/base/data/home/apps/s~takemailer/1.357442066172211834/django_bootstrap.py", line 65, in <module>
main()
File "/base/data/home/apps/s~takemailer/1.357442066172211834/django_bootstrap.py", line 62, in main
util.run_wsgi_app(application)
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 98, in run_wsgi_app
run_bare_wsgi_app(add_wsgi_middleware(application))
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/util.py", line 116, in run_bare_wsgi_app
result = application(env, _start_response)
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/core/handlers/wsgi.py", line 189, in __call__
response = self.get_response(request)
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/core/handlers/base.py", line 115, in get_response
receivers = dispatcher.send(signal=signals.got_request_exception)
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/dispatch/dispatcher.py", line 360, in send
**named
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/dispatch/robustapply.py", line 47, in robustApply
return receiver(*arguments, **named)
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/db/__init__.py", line 47, in _rollback_on_exception
transaction.rollback_unless_managed()
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/db/transaction.py", line 145, in rollback_unless_managed
connection._rollback()
File "/base/python_runtime/python_lib/versions/third_party/django-0.96/django/db/backends/dummy/base.py", line 13, in complain
raise ImproperlyConfigured, "You haven't set the DATABASE_ENGINE setting yet."
这个问题是here的后续问题,因为该问题没有得到任何答案,我不断尝试,问题归结为这个小措施: