【问题标题】:ContentType matching query does not exist. django=3.0.7ContentType 匹配查询不存在。 django=3.0.7
【发布时间】:2020-09-27 16:36:13
【问题描述】:
from django.contrib.contenttypes.models import ContentType

def posts_detail(request,slug=None):
     instance = get_object_or_404(Post, slug=slug)
     if instance.publish > datetime.datetime.now().date() or instance.draft:
          if not request.user.is_staff or not request.user.is_superuser:
               raise Http404
     share_string = quote_plus(instance.content)

initial_data = {
        "content_type": instance.get_content_type,
        "object_id": instance.id
}
form = CommentForm(request.POST or None, initial=initial_data)
if form.is_valid() and request.user.is_authenticated:
    c_type = form.cleaned_data.get("content_type")
    content_type = ContentType.objects.get(model=c_type)
    obj_id = form.cleaned_data.get('object_id')
    content_data = form.cleaned_data.get("content")
    new_comment, created = Comment.objects.get_or_create(
                        user = request.user,
                        content_type= content_type,
                        object_id = obj_id,
                        content = content_data,

                    )
    if created:
        print("yeah it worked")



comments = instance.comments
context = {
    "title": instance.title,
    "instance": instance,
    "share_string": share_string,
    "comments": comments,
    "comment_form":form,
}
return render(request, "post_detail.html", context)

显示以下错误

ContentType 匹配查询不存在。 请求方法:POST 请求网址:http://127.0.0.1:8000/posts/gchgjvhbk/ Django 版本:3.0.7 异常类型:DoesNotExist 异常值:
ContentType 匹配查询不存在。 异常位置:C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37\lib\site-packages\django\db\models\query.py 在 get,第 417 行 Python 可执行文件:C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37\python.exe Python版本:3.7.6 Python 路径:
['C:\Users\ANUPYADAV\Desktop\try19\src', 'C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37\Scripts', 'C:\Users\ANUPYADAV\Desktop\try19\src', 'C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37\python37.zip', 'C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37\DLLs', 'C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37\lib', 'C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37', 'C:\Users\ANUPYADAV\A​​ppData\Roaming\Python\Python37\site-packages', 'C:\Users\ANUPYADAV\A​​ppData\Local\Programs\Python\Python37\lib\site-packages']

【问题讨论】:

    标签: django


    【解决方案1】:

    不确定get_content_type 函数在您的帖子模型中做了什么,从错误来看,它正在返回 ContentType 对象。

    ContentType 匹配查询不存在

    第一个选项: 你可以先在值中添加.model,所以它应该是这样的

    initial_data = {
            "content_type": instance.get_content_type.model,
            "object_id": instance.id
    }
    

    看看它是否有效。

    第二个选项:如果第一个选项显示错误,请删除.model,以便您保持代码不变

    initial_data = {
            "content_type": instance.get_content_type,
            "object_id": instance.id
    }
    

    并删除那些:

    c_type = form.cleaned_data.get("content_type")
    content_type = ContentType.objects.get(model=c_type)
    

    并添加其中之一

    content_type = ContentType.objects.get_for_model(instance)
    

    content_type = ContentType.objects.get(model='post')
    

    看看有没有用,希望对你有帮助……

    另外,你为什么要签署两个不同的身份验证?

    if not request.user.is_staff or not request.user.is_superuser:
    if request.user.is_authenticated:
    

    第一个 if 语句已经只允许 super 和 staff。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 2015-01-28
      • 2022-01-05
      相关资源
      最近更新 更多