gpt4 book ai didi

python - 字段 'id' 需要一个数字但得到了 'natsu' django

转载 作者:行者123 更新时间:2023-12-02 16:36:32 26 4
gpt4 key购买 nike

我想创建一个 user_posts View ,其中包含与特定用户相关的所有帖子,假设有/有用户“Natsu”撰写的博客文章,然后登录用户假设“Testuser”将能够查看该用户的所有帖子,即用户“Natsu”的所有帖子。

博客模型.py

class Post(models.Model):
title = models.CharField(max_length=250)
slug = models.SlugField(max_length=250, unique_for_date='publish')
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts')
body = models.TextField()

用户模型.py

class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
image = models.ImageField(upload_to='profile_pic', default='default.jpg')

def __str__(self):
return f'{ self.user.username } Profile'

views.py

def user_posts(request, username):
posts = Post.objects.filter(author=username)
return render(request, 'blog/user_posts.html', {'posts':posts})

user_posts.html

{% for post in posts %}
<a href="{% url 'user-posts' post.author.username %}" class="mr-2">{{ post.author }}</a>
<h2><a href="{% url 'post-detail' post.pk %}">{{ post.title }}</a></h2>
{{ post.body|truncatewords:30 }}
{% endfor %}

urls.py

  path('user/<str:username>/', views.user_posts, name='user-posts'),

但是当我转到那个 url 时,它显示错误 Field 'id' expected a number but got 'admin'.

完整的追溯在这里:

Internal Server Error: /user/natsu/
Traceback (most recent call last):
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1772, in get_prep_value
return int(value)
ValueError: invalid literal for int() with base 10: 'natsu'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Sheraram Prajapat\OneDrive\Desktop\Virtual Environment\mysite\blog\views.py", line 78, in user_posts
posts = Post.published.filter(author=username)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py", line 1350, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py", line 1311, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\sql\query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\related_lookups.py", line 115, in
get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\Sheraram Prajapat\AppData\Local\Programs\Python\Python38\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
raise e.__class__(
ValueError: Field 'id' expected a number but got 'natsu'.
[29/Jun/2020 17:47:37] "GET /user/natsu/ HTTP/1.1" 500 127097

我哪里做错了?还有其他选择吗?

最佳答案

Post 上的

author 将是 User 表的主键,它是一个整数(除非从默认值修改)而不是用户名。您首先需要查找 usernameUser 或使用其他一些 Django“魔法”。也许尝试其中之一(我认为他们都在后端做同样的事情):

posts = Post.objects.filter(author=User.object.filter(username=username)[:1])

posts = Post.objects.filter(author__username=username)

(我也没有测试...所以请随意编辑此答案或评论,如果两者都不起作用)

关于python - 字段 'id' 需要一个数字但得到了 'natsu' django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62638087/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com