好吧,我知道这意味着我的后端python代码是错误的,但我就是不知道我做错了什么。因为这个错误没有多大帮助,我很难找出问题所在。如果有人能发现,我会很感激的。在
我要做的是,当评论被提交时通知。在new_comment = Comment.objects.create_comment(
user=MyProfile.objects.get(user=request.user),
path=origin_path,
text=comment_text,
post = post
)
notify.send(request.user,recipient='poster', action='respondposter')//see this one? I'm connecting it with signal
hidden_data = {
"post_id" : post.id,
"origin_path" : request.get_full_path,
"parent_id" : None
}
comment_form = CommentForm(hidden_data=hidden_data)
html = render_to_string('main/parent_comment.html', {'comment': new_comment,
'user': request.user,
'comment_form':comment_form})
response_data = {"status":200, "message":"new comment_stored", "comment":html, 'parent': False}
return JsonResponse(response_data)
在模型.py在
^{pr2}$
有人能看出我做错了什么吗?还是这对你来说太模糊了?在
多亏了这条评论,我得到了回溯Traceback:
File "/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
58. return view_func(*args, **kwargs)
File "/comments/views.py" in comment_create_view
82. notify.send(request.user,recipient='poster', action='respondposter')
File "/env/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py" in send
201. response = receiver(signal=self, sender=sender, **named)
File "/notifications/models.py" in new_notification
20. new_notification_create = Notification.objects.create(recipient=recipient, action=action)
File "/env/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/env/local/lib/python2.7/site-packages/django/db/models/query.py" in create
346. obj = self.model(**kwargs)
File "/env/local/lib/python2.7/site-packages/django/db/models/base.py" in __init__
468. setattr(self, field.name, rel_obj)
File "/env/local/lib/python2.7/site-packages/django/db/models/fields/related.py" in __set__
635. self.field.rel.to._meta.object_name,
Exception Type: ValueError at /comment/create/
Exception Value: Cannot assign "'poster'": "Notification.recipient" must be a "MyProfile" instance.
Request information:
GET: No GET data
编辑:def comment_create_view(request):
if request.method == "POST" and request.user.is_authenticated():
parent_id = request.POST.get('parent_id')
post_id = request.POST.get("post_id")
origin_path = request.POST.get("origin_path")
try:
post = Post.objects.get(id=post_id)
except:
response_dat = {"code":400,"message":"Post does not exists"}
return JsonResponse(response_data)
parent_comment = None
if parent_id is not None:
try:
parent_comment = Comment.objects.get(id=parent_id)
except:
parent_comment = None
if parent_comment is not None and parent_comment.post is not None:
post = parent_comment.post
form = CommentForm(data=request.POST)
if form.is_valid():
comment_text = form.cleaned_data['comment']
if parent_comment is not None:
# parent comments exists
new_comment = Comment.objects.create_comment(
user=MyProfile.objects.get(user=request.user),
path=parent_comment.get_origin,
text=comment_text,
post = post,
parent=parent_comment
)
notify.send(request.user,recipient='parent_comment.user', action='respondtouser')
hidden_data = {
"post_id" : post.id,
"origin_path" : request.get_full_path,
"parent_id" : parent_comment.id
}
comment_form = CommentForm(hidden_data=hidden_data)
html = render_to_string('main/child_comment.html', {'comment': [new_comment],
'user': request.user,
'comment_form':comment_form})
response_data = {"status":200, "message":"comment_stored",
"comment":html,
'parent': True,
'parent_id': parent_comment.id,
'comment_count': parent_comment.comment_count()}
return JsonResponse(response_data)
else:
new_comment = Comment.objects.create_comment(
user=MyProfile.objects.get(user=request.user),
path=origin_path,
text=comment_text,
post = post
)
notify.send(request.user,recipient='Post.moderator', action='respondposter')
hidden_data = {
"post_id" : post.id,
"origin_path" : request.get_full_path,
"parent_id" : None
}
comment_form = CommentForm(hidden_data=hidden_data)
html = render_to_string('main/parent_comment.html', {'comment': new_comment,
'user': request.user,
'comment_form':comment_form})
response_data = {"status":200, "message":"new comment_stored", "comment":html, 'parent': False}
return JsonResponse(response_data)
else:
print str(form)
messages.error(request, "There was an error with your comment.")
response_data = {"status":400,"message":"There was an error with your comment."}
return JsonResponse(response_data)
else:
raise Http404
我得到家长的意见不是我的个人资料,后版主不是我的个人资料