我必须为大学作业制作一个项目博客,并试图制作一个页面来显示特定用户的所有帖子,但我一直收到此错误


  追溯:
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/core/handlers/exception.py”
  在内部
    34. response = get_response(请求)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/core/handlers/base.py”
  在_get_response中
    115. response = self.process_exception_by_middleware(e,request)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/core/handlers/base.py”
  在_get_response中
    113. response = wrapd_callback(request,* callback_args,** callback_kwargs)
  
  在user_blog_list中的文件“ /home/Grayocean/grayocean.co/blog/views.py”
    121. blogs = get_object_or_404(博客,作者=用户名)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/shortcuts.py”
  在get_object_or_404中
    93. return queryset.get(* args,** kwargs)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/query.py”
  在得到
    399. clone = self.filter(* args,** kwargs)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/query.py”
  在过滤器中
    892. return self._filter_or_exclude(False,* args,** kwargs)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/query.py”
  在_filter_or_exclude中
    910. clone.query.add_q(Q(* args,** kwargs))
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/sql/query.py”
  在add_q中
    1290.子句,_ = self._add_q(q_object,self.used_aliases)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/sql/query.py”
  在_add_q中
    1318. split_subq = split_subq,simple_col = simple_col,
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/sql/query.py”
  在build_filter中
    1251. condition = self.build_lookup(lookups,col,value)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/sql/query.py”
  在build_lookup中
    1116. lookup = lookup_class(lhs,rhs)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/lookups.py”
  初始化
    20. self.rhs = self.get_prep_lookup()
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/fields/related_lookups.py”
  在get_prep_lookup中
    115. self.rhs = target_field.get_prep_value(self.rhs)
  
  文件
  “ /home/Grayocean/.virtualenvs/myenv/lib/python3.7/site-packages/django/db/models/fields/init.py”
  在get_prep_value中
    966. return int(value)
  
  异常类型:/ user / rheazes处的ValueError异常值:无效
  以10为基数的int()的文字:'testuser'


views.py

def user_blog_list(self, username):
    blogs=get_object_or_404(Blog, author=username)
    context={
    'posts':blogs
    }
    return render(request,'blog/user_posts.html',context)


models.py

class Blog(models.Model):
    title=models.CharField(max_length=100)
    content=models.TextField(blank=True)
    image = models.ImageField(upload_to='blog_images', blank=True)
    image1= models.ImageField(upload_to='blog_images', blank=True,verbose_name='second image')
    image2= models.ImageField(upload_to='blog_images', blank=True,verbose_name='third image')
    image3= models.ImageField(upload_to='blog_images', blank=True,verbose_name='fourth image')
    image4= models.ImageField(upload_to='blog_images', blank=True,verbose_name='fifth image')
    video =models.FileField(upload_to='blog_images',blank=True)
    date_posted=models.DateTimeField(default=timezone.now)
    author=models.ForeignKey(User, on_delete=models.CASCADE)
    likes=models.ManyToManyField(User,related_name='likes',blank=True)
    dislikes=models.ManyToManyField(User,related_name='dislikes',blank=True)


urls.py

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

最佳答案

您在get_object_or_404()函数中使用了错误的查找表达式。它应该是author__username=username而不是author=username

def user_blog_list(self, username):
    blogs=get_object_or_404(Blog, author__username=username) # change is here <<<<
    context={
    'posts':blogs
    }
    return render(request,'blog/user_posts.html',context)

关于python - int()的基数为10的文字:“testuser”,即使传递的参数是字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56611091/

10-11 16:21