简体   繁体   English

匹配查询不存在。 Django 错误

[英]matching query does not exist. Django Error

this is my code for my project, I just get this error I tried to figure it out but I don't get it,这是我的项目代码,我刚刚得到这个错误我试图弄清楚但我不明白,

Django Error:姜戈错误:

DoesNotExist at /save_post/不存在于 /save_post/

Profile matching query does not exist.配置文件匹配查询不存在。

views.py, line 75, in save_post form.authore = Profile.objects.get(user=request.user) views.py, line 75, in save_post form.authore = Profile.objects.get(user=request.user)

views.py视图.py

    @login_required
    def save_post(request):
        if request.method == "POST":
            form = Post(content=request.POST['content'])
            form.authore = Profile.objects.get(user=request.user)
            form.save()
        elif request.method == "PUT":
            data = json.loads(request.body)
            post_id = int(data["post_id"])
            new_content = data["new_content"]
            post = Post.objects.filter(id=post_id).first()
            if post.authore.user != request.user:
                return HttpResponse(status=401)
            post.content = new_content
            post.save()
            return JsonResponse({
                "result": True
            }, status=200)
        else:
            return JsonResponse({
                "error": "post not found"
            }, status=400)
        return index(request)

models.py模型.py

    class User(AbstractUser):
    pass


class Profile(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)


class Post(models.Model):
    content = models.TextField()
    timestamp = models.DateTimeField(default=timezone.now)
    authore = models.ForeignKey(Profile, on_delete=models.CASCADE)
    likes = models.PositiveIntegerField(default=0, blank=True, null=True)

    def serialize(self):
        return {
            "id": self.id,
            "content": self.content,
            "timestamp": self.timestamp.strftime("%b %#d %Y, %#I:%M %p"),
            "authore": self.authore.id,
            "username": self.authore.user.username,
            "likes": self.likes.count(),
        }

this seems correct but you already this user you are using doesn't have profile so here my notes :这似乎是正确的,但您已经使用的这个用户没有个人资料,所以这里是我的笔记:

You can use get_object_or_404 this if profile not found will return not found您可以使用get_object_or_404如果未找到配置文件将返回未找到

from django.shortcuts import  get_object_or_404
form.authore =  get_object_or_404(Profile, user=request.user)

when user-created no logic here profile must created so it is logical user doesn't have profile当用户创建没有逻辑在这里配置文件必须创建所以它是逻辑用户没有配置文件

it is better to use one_to_one relation ship when creating user profile as each user has only one profile and vice versa创建用户配置文件时最好使用 one_to_one 关系,因为每个用户只有一个配置文件,反之亦然

to make it correct working refer here https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html要使其正确工作,请参阅此处https://simpleisbetterthancomplex.com/tutorial/2016/07/22/how-to-extend-django-user-model.html

go to Extending User Model Using a One-To-One Link in previous link转到上一个链接中使用一对一链接扩展用户模型

field execute this tutorial and user profile will created auto when user created字段执行本教程,用户配置文件将在用户创建时自动创建

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CustomUser 匹配查询不存在。 错误(DJANGO) - CustomUser matching query does not exist. ERROR( DJANGO) 匹配查询不存在。 在 django - matching query does not exist. in django “……匹配查询不存在。”错误,但对象显然存在 - “… matching query does not exist.” error, but object clearly does exist Django Ajax请求-500个内部服务器错误,带有“不存在匹配的查询。”错误 - Django Ajax requests - 500 INTERNAL SERVER ERROR with “matching query does not exist.” error Django 错误“home.models.Friend.DoesNotExist:好友匹配查询不存在。” - Django error "home.models.Friend.DoesNotExist: Friend matching query does not exist." 房间匹配查询不存在。 django rest 框架,Django retsframework - Room matching query does not exist. django rest framework, Django retsframework / accounts / register /上的DidsNotExist不存在网站匹配查询。 (Django,Python) - DoesNotExist at /accounts/register/ Site matching query does not exist. (django, python) clients.models.Clients.DoesNotExist:客户端匹配查询不存在。 - django python - clients.models.Clients.DoesNotExist: Clients matching query does not exist. - django python Django - 管理区域 - 我无法从用户中删除用户(用户匹配查询不存在。) - Django - Admin Area - I can't delete a user from users (User matching query does not exist.) 错误:MyUser匹配查询不存在-Django - Error: MyUser matching query does not exist - Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM