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

时间:2018-09-19 10:31:42

标签: python django django-views kwargs django-2.1

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

这是我的代码,使用“用户名”作为kwarg,返回键错误,

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'

1 个答案:

答案 0 :(得分:1)

URL参数名称不匹配

鉴于我正确地理解了您的问题,您将用户名作为标头传递给了视图,例如:

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模型的UserUser与它的文本表示形式不同(例如,通过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

话虽这么说,最好使用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>