我有一个在谷歌应用引擎上运行的网站,并希望包含一个联系表格。 我的app.yaml看起来像这样:
version: 1
runtime: python27
api_version: 1
threadsafe: true
handlers:
- url: /
static_files: www/index.html
upload: www/index.html
- url: /(.*)
static_files: www/\1
upload: www/(.*)
这对静态文件工作正常,但我如何包含联系表单的py文件?
我已经尝试使用此app.yaml文件运行它:
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: jinja2
version: latest
- name: webapp2
version: latest
handlers:
- url: /
static_files: www/index.html
upload: www/index.html
- url: /(.*)
static_files: www/\1
upload: www/(.*)
- url: /.*
script: www/contactForm.app
但它不起作用,不发送电子邮件 我的py文件看起来像这样:
import webapp2
import jinja2
import os
from google.appengine.api import mail
jinja_environment = jinja2.Environment(autoescape=True,loader=jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')))
class contact(webapp2.RequestHandler):
template = jinja_environment.get_template('contact.html')
def get(self):
self.response.out.write(self.template.render())
def post(self):
# takes input from user
vorname=self.request.get("vorname")
...
message=mail.EmailMessage(sender="authorized sender address",subject="Kontaktformular")
if not mail.is_email_valid(userMail):
self.response.out.write("Wrong email! Check again!")
message.to="..."
message.body=""" Hallo:
Vorname: %s
...
Text: %s""" %(vorname,...,text)
message.send()
self.response.out.write("Message sent!")
app = webapp2.WSGIApplication([('/contact',contact)], debug=True)
有人知道如何让它发挥作用吗?
答案 0 :(得分:2)
您的联系表单处理程序未被点击,因为您之前有一个包含所有权限的规则。此外,您的联系表单处理程序需要自己的URL,而不是具有catch-all模式。试试这个:
version: 1
runtime: python27
api_version: 1
threadsafe: true
libraries:
- name: jinja2
version: latest
- name: webapp2
version: latest
handlers:
- url: /
static_files: www/index.html
upload: www/index.html
- url: /contact
script: www/contactForm.app
- url: /(.*)
static_files: www/\1
upload: www/(.*)
此外,由于缺少用于缩进代码的制表符/空格,您的Python似乎无效。应该更像是:
import webapp2
import jinja2
import os
from google.appengine.api import mail
jinja_environment = jinja2.Environment(autoescape=True,
loader=jinja2.FileSystemLoader(
os.path.join(os.path.dirname(__file__),
'templates')))
class contact(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('contact.html')
self.response.out.write(self.template.render())
def post(self):
# takes input from user
vorname=self.request.get("vorname")
# ...
if not mail.is_email_valid(userMail):
self.response.out.write("Wrong email! Check again!")
message=mail.EmailMessage(sender="authorized sender address",
subject="Kontaktformular")
message.to="..."
message.body=""" Hallo:
Vorname: %s
...
Text: %s""" %(vorname,...,text)
message.send()
self.response.out.write("Message sent!")
app = webapp2.WSGIApplication([('/contact',contact)], debug=True)