我已经做了很多研究试图自己解决这个问题,但我的解决方案还没有达成。如果有人能给我任何指针或帮助,我会非常感激。
我正在创建一个具有基本博客功能的博客,例如;喜欢,不喜欢,发帖,评论,多个用户,CRUD功能等。到目前为止,我已经能够克服我的大部分问题,而且我即将完成这个项目。
我遇到的最后一点麻烦是我的Edit Comment
功能,我可以删除评论,但如果我尝试更新评论,我会收到以下错误:
ERROR 2017-03-08 14:26:21,907 wsgi.py:279]
Traceback (most recent call last):
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/google/appengine/runtime/wsgi.py", line 267, in Handle
result = handler(dict(self._environ), self._StartResponse)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 1519, in __call__
response = self._internal_error(e)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/lib/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/home/crow/UdacityProjects/blog/blog.py", line 424, in post
comment.text = self.request.get('comment_text')
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/google/appengine/ext/db/__init__.py", line 617, in __set__
value = self.validate(value)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/google/appengine/ext/db/__init__.py", line 2810, in validate
value = super(UnindexedProperty, self).validate(value)
File "/home/crow/Downloads/google-cloud-sdk/platform/google_appengine/google/appengine/ext/db/__init__.py", line 644, in validate
raise BadValueError('Property %s is required' % self.name)
BadValueError: Property text is required
现在我不确定为什么会这样,我查找了其他人为类似问题找到的解决方案,似乎没有一个适用于我的情况。以下是我的相关代码供参考:
EditComment(BlogHandler)
class EditComment(BlogHandler):
def get(self, post_id, comment_id):
post = Post.get_by_id(int(post_id), parent=blog_key())
comment = Comment.get_by_id(int(comment_id))
if comment:
if comment.user.name == self.user.name:
self.render("editcomment.html", comment_text=comment.text)
else:
error = "You may only edit your own comments"
self.render("editcomment.html", edit_error=error)
else:
error = "This comment no longer exists"
self.render("editcomment.html", edit_error=error)
def post(self, post_id, comment_id):
if self.request.get("update_comment"):
comment = Comment.get_by_id(int(comment_id))
if comment.user.name == self.user.name:
comment.text = self.request.get('comment_text')
comment.put()
time.sleep(0.1)
self.redirect('/post/%s' % str(post_id))
else:
error = "you may only edit your own comments"
self.render(
"editcomment.html",
comment_text=comment.text,
edit_error=error)
elif self.request.get("cancel"):
self.redirect('/post/%s' % str(post_id))
Comment(db.model)
class Comment(db.Model):
user = db.ReferenceProperty(User, required=True)
post = db.ReferenceProperty(Post, required=True)
created = db.DateTimeProperty(auto_now_add=True)
text = db.TextProperty(required=True)
@classmethod
def cdb_blog_id(cls, blog_id):
c = Comment.all().filter('post =', blog_id)
return c.count()
@classmethod
def adb_blog_id(cls, blog_id):
c = Comment.all().filter('post =', blog_id).order('created')
return c
editcomment.html
{% extends "base.html" %}
{% block content %}
<div class="twelve columns">
<h3>Edit Comment</h3>
<hr>
<div class="row">
<div class="twelve columns">
<form method="post">
<label>
<textarea class="u-full-width" name="text" id="texta2">{{comment_text}}</textarea>
</label>
<div class="error">{{edit_error}}</div>
<input type="submit" class="button" name="update_comment" value="Update">
<input type="submit" class="button" name="cancel" value="Cancel">
</form>
</div>
</div>
</div>
{% endblock %}
现在回溯没有指向我的代码中导致问题的区域,因此我将在此处提供__inti__.py
文件的相关行:
Line 617
def __set__(self, model_instance, value):
"""Sets the value for this property on the given model instance.
See http://docs.python.org/ref/descriptors.html for a description of
the arguments to this class and what they mean.
"""
value = self.validate(value)
setattr(model_instance, self._attr_name(), value)
Line 2810
raise BadValueError('Property %s must be convertible '
'to a %s instance (%s)' %
(self.name, self.data_type.__name__, err))
value = super(UnindexedProperty, self).validate(value)
if value is not None and not isinstance(value, self.data_type):
raise BadValueError('Property %s must be a %s instance' %
(self.name, self.data_type.__name__))
return value
Line 644
if self.empty(value):
if self.required:
raise BadValueError('Property %s is required' % self.name)
我不确定我在这里做错了什么,如果有人能给我任何建议,我将非常感激。再次感谢
答案 0 :(得分:1)
该错误表示您从self.request.get('comment_text')
获得并传递给comment.text
的值无效 - None
。
检查已完成,因为text
属性在required
模型中将True
选项设置为Comment
。
因此请检查您是否正确传递了'comment_text'
请求参数(或其值)。
也许您需要name="comment_text"
以下一行中的editcomment.html
?
<textarea class="u-full-width" name="text" id="texta2">{{comment_text}}</textarea>