代码应打印特定用户的帖子,但它是所有用户的帖子

时间:2019-02-24 15:50:17

标签: django

            override func viewDidLoad() {
                    super.viewDidLoad()


         itemEntryRef.order(by: "_id", descending: false).getDocuments() {(querySnapshot, err) in
                    print("query attempted")


                    if let err = err {
                        // There was an error
                        print("query failed")
                        print(err)

                    } else {

                        print("query successful")

                        print(querySnapshot!.count)
                        self.itemCount = querySnapshot!.count

                    }
            }

     //UIcollectionView DataSource
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

            return self.itemCount
        }

        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            let cell = ledgerCollectionView.dequeueReusableCell(withReuseIdentifier: testString, for: indexPath)
            cell.backgroundColor = UIColor(named: "translucent")
            cell.layer.borderWidth = 0.5

            return cell
        }

此代码应打印特定用户的帖子,但它正在打印所有用户的帖子,此代码出了点问题,但我没有得到它。我尝试了其他组合但也无法使用。有人可以帮忙吗? 预先感谢

1 个答案:

答案 0 :(得分:0)

您应按以下方式更改此设置。您可以通过self.request.user内部方法中的get_queryset来过滤帖子。

class UserPosts(generic.ListView):
    model = models.Post
    template_name = 'posts/user_post_list.html'

    def get_query_set(self):
        if self.request.user.is_authenticated:
            return self.model.objects.filter(user=self.request.user)    # filter posts by current user
        else:
            raise Http404