gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:01:50 25 4
gpt4 key购买 nike

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

这是我的代码,使用“用户名”作为 kwargs,即返回 keyerror,

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

url.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 参数名称不匹配

鉴于我正确理解您的问题,您将用户名作为别名传递给 View ,例如:

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 类有一个 User 模型的 ForeignKeyUser 与其文本表示形式不同(例如通过用户名),因此过滤器将如下所示:

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>

关于python - 如何在 Django 过滤器的 kwargs 中传递用户名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52403794/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com