我需要一些帮助。当我第一次加载我的网页时,MainPage中的输入错误会一直显示在我的网页中。我如何解决它,所以当我加载页面时,它确实显示任何输入错误?
错误应仅在用户未输入任何文本并单击“共享评论”按钮或用户仅为文本放置空格并单击按钮时显示。
这是我的代码和HTML:
代码:
from google.appengine.ext import ndb
from google.appengine.api import users
import os
import time
import jinja2
import webapp2
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_environment = jinja2.Environment(
loader = jinja2.FileSystemLoader(template_dir),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class Author(ndb.Model):
identity = ndb.StringProperty(indexed=True)
name = ndb.StringProperty(indexed=False)
email = ndb.StringProperty(indexed=False)
class Comment(ndb.Model):
author = ndb.StructuredProperty(Author)
content = ndb.StringProperty(indexed=False)
timestamp = ndb.DateTimeProperty(auto_now_add=True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
class PostComments(Handler):
def post(self):
comment = Comment()
if users.get_current_user():
comment.author = Author(
identity=users.get_current_user().user_id(),
name=users.get_current_user().nickname(),
email=users.get_current_user().email())
comment.content = self.request.get('comment')
delay = 1
if comment.content == '':
self.redirect('/error#comments')
elif comment.content.isspace() == True: #checks for whitespace
self.redirect('/error#comments')
else:
comment.put()
time.sleep(delay)
self.redirect('/#comments')
class MainPage(Handler):
def get(self):
query_result = 25
comments_query = Comment.query().order(-Comment.timestamp)
comments, cursor, more = comments_query.fetch_page(query_result)
user = users.get_current_user()
if user:
url = users.create_logout_url(self.request.uri)
url_linktext = 'Logout'
else:
user = 'Anonymous Poster'
url = users.create_login_url(self.request.uri)
url_linktext = 'Login'
template_values = {
'user': user,
'comments': comments,
'url': url,
'url_linktext': url_linktext,
'blank_comment_error': 'Please write a valid comment & resubmit. Thank you!',
}
template = jinja_environment.get_template('page_body.html')
self.write(template.render(template_values))
app = webapp2.WSGIApplication([
('/', MainPage),
('/sign', PostComments),
('/error', MainPage),
], debug=True)
我的HTML的一部分:
<div class="title">
<h2>User Comments</h2>
</div>
<br>
<a name="comments"></a>
<div class="comments">
<form action="/sign" method="post">
<div>
<label for="comment">Add a Comment Please!</label>
<a class="linktext" href="{{ url|safe }}">{{ url_linktext }}</a>
</div>
<div class="error"><p>{{ blank_comment_error }}</p>
</div>
<div><p class="img"><textarea name="comment" rows="5" cols="80"></textarea></p>
</div>
<div><input type="submit" value="Share Comment">
</div>
</form>
<br>
<div class="comments">
{% for comment in comments %}
{% if comment.author %}
<b>{{ comment.author.email }}
{% if user %}
(You)
{% endif %}
</b> wrote on {{ comment.timestamp.strftime("%m/%d/%Y at %H:%M UTC") }}:
{% else %}
<em>Anonymous</em> Wrote on {{ comment.timestamp.strftime("%m/%d/%Y at %H:%M UTC") }}:
{% endif %}
<div class="comment"><blockquote>{{ comment.content }}</blockquote></div>
{% endfor %}
</div>
</div>
答案 0 :(得分:1)
您总是会看到错误,因为get()
始终设置错误。
def get(self):
...
template_values = {
...
'blank_comment_error': 'Always seen',
...
}
...
self.write(template.render(template_values))
看起来你试图通过添加#comments
来表示存在错误,但主/错误页面的处理程序中没有任何内容检查它。