如何在 Django 过滤器的 kwargs 中传递用户名?

How to pass username in kwargs of Django filter?

在网络应用程序中,要从我使用用户 pk 的特定用户检索所有对象。但是为了使 url 更具可读性,我想使用用户名。问题出在 django 视图中,kwargs 中的用户 pk 给出了正确的值,但是当我使用用户名时它显示错误。

这是我使用 'username' 作为 kwargs 的代码,即返回 keyerror,

views.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'])

urls.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/<b><str:slug></b>/questions/',
    views.UserAllQuestionView.as_view(),
    name='user_profile_question_all'
),

然而你将此参数命名为slug,但在你看来,你调用的是self.kwargs['username']。因此,您需要更改两者之一。例如:

path(
    'm/user/<b><str:username></b>/questions/',
    views.UserAllQuestionView.as_view(),
    name='user_profile_question_all'
),

此外,它可能仍然无法正常工作。如果我理解正确的话,你的 Question class 有一个 ForeignKeyUser 模型。 User 与其文本表示不同(例如通过 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(<b>user__username</b>=self.kwargs['username'])

改用 user_id

话虽这么说,但使用 Userid 可能会更好,这样可能会减少混淆(例如,如果用户设法使用带有斜杠,那么 URL 将不再有效)。所以更安全的方法可能是:

path(
    'm/user/<b><int:userid></b>/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(<b>user_id=self.kwargs['userid']</b>)

并在模板中这样写:

<a href="<b>{% url 'mechinpy:user_profile_question_all' userid=user.id %}</b>">All User Questions</a>