python - 如何在 Django 过滤器的 kwargs 中传递用户名?
问题描述
在 webapp 中,要检索来自特定用户的所有对象,我使用的是用户 pk。但是为了使 url 更具可读性,我想使用用户名。问题出在 django 视图中,用户 pk 在 kwargs 中给出了正确的值,但是当我使用用户名时它显示错误。
这是我使用“用户名”作为 kwargs 的代码,即返回 keyerror,
视图.py
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'])
网址.py
path('m/user/<str:slug>/questions/', views.UserAllQuestionView.as_view(), name='user_profile_question_all'),
html
<a href="{% url 'mechinpy:user_profile_question_all' user.username %}">All User Questions</a>
追溯:
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 传递给视图,例如:
path(
'm/user/<str:slug>/questions/',
views.UserAllQuestionView.as_view(),
name='user_profile_question_all'
),
但是,您命名此参数slug
,但在您看来,您调用self.kwargs['username']
. 因此,您需要更改两者之一。例如:
path(
'm/user/<str:username>/questions/',
views.UserAllQuestionView.as_view(),
name='user_profile_question_all'
),
此外,它可能仍然无法正常工作。如果我理解正确的话,你的Question
班级就有一个ForeignKey
模型User
。AUser
与其文本表示不同(例如通过 a username
),因此过滤器将如下所示:
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'])
user_id
改用_
话虽如此,使用 the 可能会更好id
,User
这可能会减少混乱(例如,如果用户设法使用带有斜杠的用户名,那么 URL 将不再有效)。所以更安全的方法可能是:
path(
'm/user/<int:userid>/questions/',
views.UserAllQuestionView.as_view(),
name='user_profile_question_all'
),
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'])
并在模板中这样写:
<a href="{% url 'mechinpy:user_profile_question_all' userid=user.id %}">All User Questions</a>