Hey Guys我正在一个网站上工作,我已经在我的网站上添加了一个留言簿,它正在google appspot上运行并运行,但是有一个问题。它允许人们提交空白内容。我需要一个代码来告诉用户此提交无效。我将分享我的代码,任何帮助将不胜感激。这是我的第一次编码体验和我的第一个网站。 我放了一个if语句,但它不起作用。谢谢你帮帮我们。
import os
import urllib
import jinja2
import webapp2
from google.appengine.api import users
from google.appengine.ext import ndb
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
DEFAULT_GUESTBOOK_NAME = 'default_guestbook'
def guestbook_key(guestbook_name=DEFAULT_GUESTBOOK_NAME):
return ndb.Key('Guestbook', guestbook_name)
class Author(ndb.Model):
identity = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=False)
class Greeting(ndb.Model):
author = ndb.StructuredProperty(Author)
content = ndb.StringProperty(indexed=False)
date = ndb.DateTimeProperty(auto_now_add=True)
class MainPage(webapp2.RequestHandler):
def get(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greetings_query = Greeting.query(
ancestor=guestbook_key(guestbook_name)).order(-Greeting.date)
greetings = greetings_query.fetch(10)
user = users.get_current_user()
if user:
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'user': user,
'greetings': greetings,
'guestbook_name': urllib.quote_plus(guestbook_name),
'url': url,
'url_linktext': url_linktext,
}
template = JINJA_ENVIRONMENT.get_template('index.html')
self.response.write(template.render(template_values))
class Guestbook(webapp2.RequestHandler):
def post(self):
guestbook_name = self.request.get('guestbook_name',
DEFAULT_GUESTBOOK_NAME)
greeting = Greeting(parent=guestbook_key(guestbook_name))
if users.get_current_user():
greeting.author = Author(
identity=users.get_current_user().user_id(),
email=users.get_current_user().email())
greeting.content = self.request.get('content')
greeting.put()
greeting.content = self.request.get('content')
if greeting:
greeting.put()
self.redirect('/')
else:
self.redirect('/?error=That seems invalid to me.')
query_params = {'guestbook_name': guestbook_name}
self.redirect('/?' + urllib.urlencode(query_params))
app = webapp2.WSGIApplication([('/', MainPage), ('/sign', Guestbook), ], debug=True)
答案 0 :(得分:0)
你必须检查内容是否为空。
相反,您正在检查问候对象是否存在。如下条件如下
if users.get_current_user():
greeting.author = Author(
identity=users.get_current_user().user_id(),
email=users.get_current_user().email())
greeting.content = self.request.get('content')
if len(greeting.content > 0):
greeting.put()
self.redirect('/')
else:
self.redirect('/?error=That seems invalid to me.')
query_params = {'guestbook_name': guestbook_name}
self.redirect('/?' + urllib.urlencode(query_params))
答案 1 :(得分:0)
您应该检查内容是否空虚,而不是greeting
本身,并在将可能空的问候语放入存储空间之前执行此操作:
content = self.request.get('content')
if content:
greeting.content = content
greeting.put()
self.redirect('/')
else:
self.redirect('/?error=That seems invalid to me.')
或进行页面javascript检查,如here所述,或使用某些lib,如validate.js