How to pass username in kwargs of Django filter?
在webapp中,要从特定用户检索所有对象,我使用的是user pk。但为了使URL更易读,我想使用用户名。问题出在django视图中,kwargs中的用户pk给出了正确的值,但是当我使用username时,它显示错误。
以下是我的代码,使用"用户名"作为Kwargs,返回keyror,
VIEW
1 2 3 4 5 6 7 | class UserAllQuestionView(generic.ListView): model = Question template_name = 'mechinpy/user_profile_question.html' context_object_name = 'user_all_questions' def get_queryset(self): return Question.objects.filter(user=self.kwargs['username']) |
URLS.Py
1 | path('m/user/<str:slug>/questions/', views.UserAllQuestionView.as_view(), name='user_profile_question_all'), |
HTML
1 | All User Questions |
Traceback:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | File"C:\Users\Bidhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\exception.py" in inner 34. response = get_response(request) File"C:\Users\Bidhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response 126. response = self.process_exception_by_middleware(e, request) File"C:\Users\Bidhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\core\handlers\base.py" in _get_response 124. response = wrapped_callback(request, *callback_args, **callback_kwargs) File"C:\Users\Bidhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py" in view 68. return self.dispatch(request, *args, **kwargs) File"C:\Users\Bidhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\base.py" in dispatch 88. return handler(request, *args, **kwargs) File"C:\Users\Bidhan\AppData\Local\Programs\Python\Python35\lib\site-packages\django\views\generic\list.py" in get 142. self.object_list = self.get_queryset() File"C:\Users\Bidhan\Desktop\Startup\mysite\mechinpy\views.py" in get_queryset 454. return Question.objects.filter(user=self.kwargs['username']) Exception Type: KeyError at /m/user/bidhan/questions/ Exception Value: 'username' |
URL参数名称不匹配
如果我正确理解了您的问题,您可以将用户名作为一个slug传递给视图,例如:
1 2 3 4 5 | path( 'm/user/<str:slug>/questions/', views.UserAllQuestionView.as_view(), name='user_profile_question_all' ), |
但是,您将此参数命名为
1 2 3 4 5 | path( 'm/user/<str:username>/questions/', views.UserAllQuestionView.as_view(), name='user_profile_question_all' ), |
此外,它可能仍然不起作用。如果我理解正确的话,你的
1 2 3 4 5 6 7 | class UserAllQuestionView(generic.ListView): model = Question template_name = 'mechinpy/user_profile_question.html' context_object_name = 'user_all_questions' def get_queryset(self): return Question.objects.filter(user__username=self.kwargs['username']) |
用
也就是说,使用
1 2 3 4 5 | path( 'm/user/<int:userid>/questions/', views.UserAllQuestionView.as_view(), name='user_profile_question_all' ), |
1 2 3 4 5 6 7 | class UserAllQuestionView(generic.ListView): model = Question template_name = 'mechinpy/user_profile_question.html' context_object_name = 'user_all_questions' def get_queryset(self): return Question.objects.filter(user_id=self.kwargs['userid']) |
在模板中,写出来如下:
1 | All User Questions |