Django 评论 ValueError
Django comments ValueError
当 post 连接到 django-comments 提供的端点时,我收到错误 ValueError,如下所示,我想解决该错误:
详细消息是"Attempting go get content-type '7' and object PK '1' exists raised ValueError"。我不确定为什么会在管理员中引发此 ValueError 我能够 post 评论没有错误:
请注意,'my user' 是管理部分内容类型列表中的第 7 个选项。
在comments.py中处理端点/comments/post/的函数:
@csrf_protect
@require_POST
def post_comment(request, next=None, using=None):
"""
Post a comment.
HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
errors a preview template, ``comments/preview.html``, will be rendered.
"""
# Fill out some initial data fields from an authenticated user, if present
data = request.POST.copy()
if request.user.is_authenticated():
if not data.get('name', ''):
data["name"] = request.user.get_full_name() or request.user.get_username()
if not data.get('email', ''):
data["email"] = request.user.email
# Look up the object we're trying to comment about
ctype = data.get("content_type")
object_pk = data.get("object_pk")
if ctype is None or object_pk is None:
return CommentPostBadRequest("Missing content_type or object_pk field.")
try:
model = apps.get_model(*ctype.split(".", 1))
target = model._default_manager.using(using).get(pk=object_pk)
except TypeError:
return CommentPostBadRequest(
"Invalid content_type value: %r" % escape(ctype))
except AttributeError:
return CommentPostBadRequest(
"The given content-type %r does not resolve to a valid model." % escape(ctype))
except ObjectDoesNotExist:
return CommentPostBadRequest(
"No object matching content-type %r and object PK %r exists." % (
escape(ctype), escape(object_pk)))
except (ValueError, ValidationError) as e:
return CommentPostBadRequest(
"Attempting go get content-type %r and object PK %r exists raised %s" % (
escape(ctype), escape(object_pk), e.__class__.__name__))
2 我在自定义评论模型中定义的额外字段:
class ProfileComment(Comment):
comment_OP_uri = models.CharField(max_length=300)
comment_receiver = models.CharField(max_length=300)
在 the documentation 之后创建了相关表格:
class ProfileCommentForm(CommentForm):
comment_OP_uri = forms.CharField(max_length=300)
comment_receiver = forms.CharField(max_length=300)
def get_comment_model(self):
# Use our custom comment model instead of the default one.
return ProfileComment
def get_comment_create_data(self):
# Use the data of the superclass, and add in the custom field
data = super(ProfileComment, self).get_comment_create_data()
data['comment_OP_uri', 'comment_receiver'] = self.cleaned_data['comment_OP_uri', 'comment_receiver']
return data
通过将content_type值的值改为users.MyUser
解决
apps.get_model
需要一个 app_name 和一个 model_name
当 post 连接到 django-comments 提供的端点时,我收到错误 ValueError,如下所示,我想解决该错误:
详细消息是"Attempting go get content-type '7' and object PK '1' exists raised ValueError"。我不确定为什么会在管理员中引发此 ValueError 我能够 post 评论没有错误:
请注意,'my user' 是管理部分内容类型列表中的第 7 个选项。
在comments.py中处理端点/comments/post/的函数:
@csrf_protect
@require_POST
def post_comment(request, next=None, using=None):
"""
Post a comment.
HTTP POST is required. If ``POST['submit'] == "preview"`` or if there are
errors a preview template, ``comments/preview.html``, will be rendered.
"""
# Fill out some initial data fields from an authenticated user, if present
data = request.POST.copy()
if request.user.is_authenticated():
if not data.get('name', ''):
data["name"] = request.user.get_full_name() or request.user.get_username()
if not data.get('email', ''):
data["email"] = request.user.email
# Look up the object we're trying to comment about
ctype = data.get("content_type")
object_pk = data.get("object_pk")
if ctype is None or object_pk is None:
return CommentPostBadRequest("Missing content_type or object_pk field.")
try:
model = apps.get_model(*ctype.split(".", 1))
target = model._default_manager.using(using).get(pk=object_pk)
except TypeError:
return CommentPostBadRequest(
"Invalid content_type value: %r" % escape(ctype))
except AttributeError:
return CommentPostBadRequest(
"The given content-type %r does not resolve to a valid model." % escape(ctype))
except ObjectDoesNotExist:
return CommentPostBadRequest(
"No object matching content-type %r and object PK %r exists." % (
escape(ctype), escape(object_pk)))
except (ValueError, ValidationError) as e:
return CommentPostBadRequest(
"Attempting go get content-type %r and object PK %r exists raised %s" % (
escape(ctype), escape(object_pk), e.__class__.__name__))
2 我在自定义评论模型中定义的额外字段:
class ProfileComment(Comment):
comment_OP_uri = models.CharField(max_length=300)
comment_receiver = models.CharField(max_length=300)
在 the documentation 之后创建了相关表格:
class ProfileCommentForm(CommentForm):
comment_OP_uri = forms.CharField(max_length=300)
comment_receiver = forms.CharField(max_length=300)
def get_comment_model(self):
# Use our custom comment model instead of the default one.
return ProfileComment
def get_comment_create_data(self):
# Use the data of the superclass, and add in the custom field
data = super(ProfileComment, self).get_comment_create_data()
data['comment_OP_uri', 'comment_receiver'] = self.cleaned_data['comment_OP_uri', 'comment_receiver']
return data
通过将content_type值的值改为users.MyUser
解决
apps.get_model
需要一个 app_name 和一个 model_name