首页 > 解决方案 > 'Keyerror at /' 在列表视图的查询集中使用主键传递用户时

问题描述

模型.py

class Notes(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    title = models.CharField(max_length=100)
    content = models.TextField()
    date_added = models.DateTimeField(default=timezone.now)

视图.py

class HomepageView(ListView):
    model = Notes
    template_name = "home.html"

    def get_queryset(self):
        return Notes.objects.filter(Q(author=self.kwargs['pk']))

它指出 pk 有 KeyError,有没有办法摆脱它提前谢谢......!

标签: pythondjangolistviewdjango-querysetdjango-related-manager

解决方案


The self.kwargs contains the parameters, that are in the URL path. So in your urls.py, you should define shomething like:

# urls.py

from app.views import HomePageView

urlpatterns = [
    path('<int:pk>/', HomePageView.as_view()),
]

Then you can visit the page, with a valid primary key, so for example /14 if 14 is a valid primary key for a User.

If you want to use the logged in user instead, you should use self.request.user instead:

# in case you want to use the logged in user

django.views.generic.list import ListView
from django.contrib.auth.mixins import LoginRequiredMixin

class HomepageView(LoginRequired, ListView):
    model = Notes
    template_name = 'home.html'

    def get_queryset(self):
        return Notes.objects.filter(author=self.request.user)

Note: it might be better to use get_user_model [Django-doc] to point to the user model, since if you later change the user model, altering the foreign keys will be easier.

 

Note: You might want to consider using a DateTimeField(auto_now_add=True) instead of the models.DateTimeField(default=timezone.now), since that will make the field non-editable, etc.


推荐阅读