我的帖子功能遇到了问题。我的应用程序是设计让人们进入他们的生日,如果他们在一个月,一天和一年中输入正确的帖子说"谢谢!这是一个完全有效的日子"如果它不是一个日期,那就是帖子说"这对我,朋友"看起来并不合适,但它不会这样做,它只是每次推送提交时自我更新。在我的代码中我的帖子功能出错了还是我的get和post功能?
import webapp2
form="""
<form method="post">
What is your birthday?
<br>
<label>Month<input type="type" name="month"></label>
<label>Day<input type="type" name="day"></label>
<label>Year<input type="type" name="year"></label>
<div style="color: red">%(error)s</div>
<br>
<br>
<input type="submit">
</form>
"""
class MainPage(webapp2.RequestHandler):
def write_form(self, error=""):
self.response.out.write(form % {"error": error})
def get(self):
self.write_form()
def valid_year(year):
if year and year.isdigit():
year = int(year)
if year > 1900 and year < 2020:
return year
def valid_day(day):
if day and day.isdigit():
day = int(day)
if day > 0 and day <= 31:
return day
months = ['Janurary',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(),m) for m in months)
def valid_month(month):
if month:
short_month = month[:3].lower()
return month_abbvs.get(short_month)
def post(self):
user_month = valid_month(self.request.get('month'))
user_day = valid_day(self.request.get('day'))
user_year = valid_year(self.request.get('year'))
if not (user_month and user_day and user_year):
self.write_form("That doesn't look valid to me, friend.")
else:
self.response.out.write("Thanks! That's a totally valid day!")
app = webapp2.WSGIApplication([('/',MainPage)], debug=True)
我甚至下载了python IDLE并使用它而不是notpad ++。 当我推送提交时,我得到了:
Internal Server Error
The server has either erred or is incapable of performing the requested operation.
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\ajper_000\Desktop\engineapp\main.py", line 66, in post
user_month = valid_month(self.request.get('month'))
NameError: global name 'valid_month' is not defined
答案 0 :(得分:2)
尝试替换
<form>
带
<form method="POST">
默认情况下,表单使用GET请求,而请求处理程序需要POST请求。
编辑:您似乎遇到了多个问题:
post
方法未定义(或可能在get
方法中定义)valid_*
方法未定义 - 您需要定义它们。答案 1 :(得分:1)
您应该在valid_month,valid_year和valid_day的函数中返回true。就像这段代码一样
import webapp2
months = ['Janurary',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(), m) for m in months)
def valid_month(user_month):
if user_month:
short_month = user_month[:3].lower()
return month_abbvs.get(short_month)
return True
def valid_day(user_day):
if user_day and user_day.isdigit():
user_day = int(user_day)
if user_day > 0 and user_day <= 31:
return user_day
return True
def valid_year(user_year):
if user_year and user_year.isdigit():
user_year = int(user_year)
if user_year > 1900 and user_year < 2020:
return user_year
return True
form = """
<form method='POST'>
<h1>What is your birthday?</h1>
<br>
<label>Month
<input type="text" name="month">
</label>
<label>day
<input type="text" name="day">
</label>
<label>year
<input type="text" name="year">
</label>
<input type="submit">
</form>
"""
class MainHandler(webapp2.RequestHandler):
def get(self):
self.response.write(form)
def post(self):
user_month = valid_month(self.request.get('month'))
user_day = valid_day(self.request.get('day'))
user_year = valid_year(self.request.get('year'))
if not (user_year and user_month and user_day):
self.response.out.write(form)
self.response.out.write("Please enter valid a function")
else:
self.response.out.write('tThanks for that')
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
因为当调用函数valid_month时,它应返回True以在浏览器中显示错误消息。
答案 2 :(得分:0)
405的原因就在堆栈轨迹的底部。
private void Save()
{
List<Element> elementList = new List<Element> ();
elementList = Root [1].Elements;
foreach (Element element in elementList) {
RootElement radioElement = (RootElement)element;
user.Title = radioElement[0].Elements[radioElement.RadioSelected].Caption;
}
user.Save ();
}
表示您没有名为NameError: global name 'valid_month' is not defined
的函数。如果你不这样做,那就写一个。如果你这样做,那就发布吧。
答案 3 :(得分:0)
Alexander,我在udacity做同样的课程,我遇到了同样的问题。但是在进入添加功能以检查用户输入的阶段之前,我刚刚在教师在组合框后更改代码后立即检查了浏览器中的输出。我遇到了同样的问题:#34; 405 Method Not Allowed&#34;以及下一行的消息&#34;此资源不允许POST方法。我扫描了代码并逐字逐字地比较了视频中的代码&#34; https://www.udacity.com/course/viewer#!/c-cs253/l-48736183/m-48714317&#34;但它没有帮助。
然后我点击随附的wiki进行课程,并完全复制了我怀疑有问题的4行。突然间它奏效了。我使用不同的在线文本comaprison工具来找出有什么区别,但无法弄清楚问题是什么。我复制并粘贴以下两个版本:
版本1:无效的版本并给出了405错误:
def get(self):
self.response.out.write(form)
def post(self):
self.response.out.write("Thanks! that's a totally valid day!")
版本2:适用的版本:
def get(self):
self.response.out.write(form)
def post(self):
self.response.out.write("Thanks! That's a totally valid day!")
我无法看到这两段代码之间的差异,但即使版本1出现错误,但版本2也会提供所需的输出。虽然我已完全复制并粘贴了Notepade ++中代码的两个版本(如上所述),但我仍然使用截图,以便读者不会说我可能会出现一些缩进问题。以下是两个版本的屏幕截图: 版本1:这不起作用:
第2节:有效:
虽然目前问题已解决但我不知道是什么导致了这个问题!任何python大师都可以解释为什么?
答案 4 :(得分:0)
在类之外和类声明之前以及表单结束之后声明方法对我来说是有效的。代码是这样的。
import webapp2
form="""
<form method='POST' >
What is your birthday
<br>
<label>Month
<input type="text" name="month">
</label>
<label>day
<input type="text" name="day">
</label>
<label>year
<input type="text" name="year">
</label>
<input type="submit">
</form>
"""
months = ['January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December']
month_abbvs = dict((m[:3].lower(),m) for m in months)
def valid_month(month):
if month:
shortMonth=month[:3].lower()
return month_abbvs.get(shortMonth)
def valid_day(day):
if day and day.isdigit():
day=int(day)
if day > 0 and day<= 31:
return day
def valid_year(year):
if year and year.isdigit():
year=int(year)
if year >=1900 and year <=2020:
return year
class MainHandler(webapp2.RequestHandler):
def get(self):
# self.response.headers['Content-Type']='Text/HTML'
self.response.write(form)
def post(self):
user_moth = valid_month(self.request.get('month'))
user_day = valid_day(self.request.get('day'))
user_year = valid_year(self.request.get('year'))
#self.response.out.write("Thanks... ")
if not (user_year and user_moth and user_day):
self.response.out.write(form)
else:
self.response.out.write('tThanks for that')
app = webapp2.WSGIApplication([
('/', MainHandler),
], debug=True)
希望它会有所帮助。否则你可以将其他参数self传递给这些函数定义并使用实例变量调用它
答案 5 :(得分:0)
Python没有自动将代码扩展到本地类;你需要告诉它。
valid_month = self.valid_month(self.request.get('month'))
您将遇到的下一个错误是
TypeError: valid_month() takes exactly 1 argument (2 given)
您需要声明自己&#39;作为&#39; valid_month&#39;的第一个参数方法
def valid_month(self, month):