我一直收到内部服务器错误 - 'BadValueError:属性内容是必需的。当我将blog_table传递给模板进行渲染时,似乎就会发生这种情况。这是代码:
main.py:
import os
import webapp2
import jinja2
from google.appengine.ext import db
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(os.path.join(os.path.dirname(__file__),'templates')), autoescape = True)
class Handler(webapp2.RequestHandler):
def write(self,*a,**kw):
self.response.out.write(*a,**kw)
def render_str(self,template,**params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self,template,**kw):
self.write(self.render_str(template,**kw))
class Blog(db.Model):
subject = db.StringProperty(required = True)
content = db.TextProperty(required = True)
created = db.DateTimeProperty(auto_now_add = True)
class MainPage(Handler):
def render_front(self, subject="", content="", error=""):
blog_table = db.GqlQuery("SELECT * FROM Blog ORDER BY created DESC")
self.render("blog.html",subject = subject,content = content,error = error,blog_table = blog_table)
def get(self):
self.render_front()
def post(self):
subject = self.request.get("subject")
content = self.request.get("content")
if subject and content:
b = Blog(subject = subject, content = content)
b.put()
self.redirect("/")
else:
error = "we need both a subject and some content"
self.render_front(subject,content,error)
app = webapp2.WSGIApplication([('/', MainPage)],debug=True)
HTML:
<!DOCTYPE html>
<html>
<head>
<title>Blog</title>
</head>
<body>
<h1>Blog</h1>
<form method="post">
<label>
<div>Subject</div>
<input type="text" name="subject" value="{{subject}}">
</label>
<label>
<div>Content</div>
<textarea name="content" value="{{content}}"></textarea>
</label>
<div class="error">{{error}}</div>
<input type="submit">
</form>
<hr>
{% for post in blog_table %}
<div class="post">
<div class="subject">{{post.subject}}</div>
<pre class="content">{{post.content}}</pre>
</div>
{% endfor %}
</body>
</html>
要查看错误,请转到http://udacity-cs253-a31.appspot.com/
感谢。
答案 0 :(得分:4)
这只是猜测 - 更改了您的Blog模型并最近添加了“内容”属性。
您的数据存储区中存在一些没有内容的Blog模型的旧实例。如果您尝试加载这些,您的代码将失败。如果您使用内容加载博客实例,一切都很好。
您必须浏览数据存储区并确保博客的所有实例都有内容。