[英]Unexpected Keyword when I Pass Url Parameter
I have a view called 'Teams' that loops through different NBA teams in a dictionary and shows their name and logo.我有一个名为“Teams”的视图,它在字典中遍历不同的 NBA 球队并显示他们的名称和徽标。 When the user clicks on one of these logos, I want them to be taken to the 'TeamDetailView'.
当用户单击其中一个徽标时,我希望它们被带到“TeamDetailView”。 This should carry over the chosen team's city/name/logo, and I can see this information being passed in the URL.
这应该会保留所选团队的城市/名称/徽标,我可以看到此信息在 URL 中传递。 When I attempt to load the team's individual page, though, it gives me a type error and says that
但是,当我尝试加载团队的个人页面时,它给了我一个类型错误并说
TeamDetailView() got an unexpected keyword argument 'city'
In the local vars section, it shows my key/value pairs being passed correctly.在本地变量部分,它显示我的键/值对被正确传递。 How can I access these parameters on the team page and correct this error?
如何访问团队页面上的这些参数并更正此错误?
callback_kwargs {'city': 'Atlanta', 'logo': 'atlanta-logo.png', 'name': 'Hawks'}
Here is my view:这是我的看法:
def TeamDetailView(request):
return render(request, 'bandwagon/team.html/')
Here is my URL:这是我的 URL:
path('team/<str:city>/<str:name>/<str:logo>/', views.TeamDetailView, name='bandwagon-team'),
Here is my Template for the Teams List:这是我的团队列表模板:
{% for key, value in teams.items %}
<a class="stream-list" href="{% url 'bandwagon-team' value.city value.name value.logo %}">
<img class="stream-img" alt="The Logo for the {{ value.city }} {{ value.name }}" src="../../../media/logos/{{ value.logo }}">
<p class="name">{{value.city }} {{value.name}}</p>
</a>
{% endfor %}
Here is my Template for the Individual Team Page, which is quite basic for now until I get these parameters passed correctly:这是我的个人团队页面模板,在我正确传递这些参数之前,这是非常基本的:
{% extends 'bandwagon/base.html' %}
{% block content %}
<h1 class="article-title">Team</h1>
{% endblock content %}
Have you tried updating your TeamDetailView function to accept the url parameters?您是否尝试过更新您的 TeamDetailView function 以接受 url 参数? Something like -
就像是 -
def TeamDetailView(request, city, name, logo):
return render(request, 'bandwagon/team.html/')
As they've tell you before you're not extracting the data from the path, you're just rendering the HTML without any context:正如他们在您不从路径中提取数据之前告诉您的那样,您只是在没有任何上下文的情况下渲染 HTML:
To solve this I would get the Team filtered by the data you're getting of the path, for example:为了解决这个问题,我会根据您从路径中获取的数据过滤团队,例如:
def TeamDetailView(request, city, name, logo):
Result = Your_Model.objects.all().filter(Q(City=city, Name=name, Logo=logo))
return render(request, 'bandwagon/team.html', {'Teams': Result})
And then in your template you could do something like:然后在您的模板中,您可以执行以下操作:
{% for Team in Teams %}
# What you want to achieve
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.