如果post函数发现错误,我希望它与Get函数通信,以便将错误添加到主页。
没有模板怎么办?
这是代码,这也是该问题的解决方案视频:
https://classroom.udacity.com/courses/cs253/lessons/48756009/concepts/485326480923#
import re
import cgi
import webapp2
# html boilerplate for the top of every page
page_header = """
<!DOCTYPE html>
<html>
<head>
<title>User-Signup</title>
</head>
<body>
<h1>Signup</h1>
"""
# html boilerplate for the bottom of every page
page_footer = """
</body>
</html>
"""
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
def valid_username(username):
return username and USER_RE.match(username)
PASS_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_password(password):
return password and PASS_RE.match(password)
EMAIL_RE = re.compile(r'^[\S]+@[\S]+\.[\S]+$')
def valid_email(email):
return not email or EMAIL_RE.match(email)
class MainHandler(webapp2.RequestHandler):
def get(self):
add_username = """
<form action="/" method="post">
<label>
Username <input type="text" name="user_name" value = ""/>
</label>
</form>
"""
add_password = """
<form action="/" method="post">
<label>
Password <input type="password" name ="password" value = ""/>
</label>
</form>
"""
validate_password = """
<form action="/" method="post">
<label>
Password <input type="password" name ="password" value = ""/>
</label>
</form>
"""
# error = self.request.get("error")
# if error:
# error_esc = cgi.escape(error, quote=True)
# error_element = '<p class="error">' + error_esc + '</p>'
# else:
error_element = ''
password_error = ''
content = page_header + add_username + error_element + add_password + password_error + page_footer
self.response.write(content)
def post(self):
error_element = ''
have_error = False
user_name = self.request.get("user_name")
password = self.request.get("password")
params = dict("")
if not valid_username(user_name):
error_element = "thats not a valid username"
have_error = True
if not valid_password(password):
password_error = "thats not a valid password"
have_error = True
add_username = """
<form method="post">
<label>
Username <input type="text" name="user_name" value = "{0}"/>
</label>
</form>
""".format(user_name)
add_password = """
<form method="post">
<label>
Password <input type="password" name ="password" value = "{0}"/>
</label>
</form>
""".format(password)
validate_password = """
<form action="/" method="post">
<label>
Password <input type="password" name ="password" value = "{0}"/>
</label>
</form>
""".format(password_error)
content = page_header + add_username + error_element + add_password + password_error + page_footer
self.response.write(content)
基本上,我需要能够从Get函数中的Post函数中引用变量。我不允许使用模板。
我尝试过的
我尝试使用带错误的重定向。但是,这似乎并不是视频中的处理方式。
请帮我。
最佳答案
您链接的视频位于登录页面的后面,但是听起来您想重定向到MainHandler.get()的url,并将错误或错误代码编码为url上的请求参数。看起来像这样:
# in your post():
def post(self):
# your code here...
if have_error:
self.redirect(url + '?error=Your_Error_Here')
else:
self.response.write(content)
# in your get():
def get(self):
error = self.request.get('error', None)
if error is not None:
# handle your error here
else:
# no error to handle
当您希望http POST处理程序将信息转发到http GET处理程序时,通常有两个选择:
如上所示,使用URL上编码的信息(即
?error=Your_Error_Here
部分)重定向到适当的URL。我经常看到它用于错误警报,如果表单成功发布,也用于确认消息(点击“保存”后,在屏幕上绿色勾号)实际上,您可以从后处理程序中调用get()处理程序,并使用其中发出的任何响应对象进行响应,而不是从后处理程序发送响应。通常,我不建议这样做。
关于python - HTML,Python,初学者。尝试从post函数引用变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43295438/