简体   繁体   English

当我通过 Url 参数时出现意外的关键字

[英]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.

相关问题 导航到一个 url 时,我收到错误:“urlopen() 得到了一个意外的关键字参数‘headers’” - when navigating to an url, I'm getting error: "urlopen() got an unexpected keyword argument 'headers' " TypeError:当我将变量从视图传递到表单时,__init__() 得到了一个意外的关键字参数“bot_id” - TypeError: __init__() got an unexpected keyword argument 'bot_id' when i pass a variable from views to form 如何在没有“lambda”关键字的情况下将方法作为函数参数传递? - How can I pass method as function parameter without 'lambda' keyword? 当有一个问号时,如何将url作为url参数传递? - How to pass a url as a url parameter when there is a question mark in it? url()得到了一个意外的关键字参数&#39;namespace&#39; - url() got an unexpected keyword argument 'namespace' 将 url 参数传递给序列化程序 - pass url parameter to serializer URL麻烦传递参数 - URL trouble to pass parameter 当它是“自我”时应该传递什么参数 - What parameter should I pass when it is “self” 打开字典以作为关键字参数传递时,如何将键映射到其他命名的关键字参数? - When unpacking a dictionary to pass as keyword arguments, how can I map a key to a differently named keyword argument? 从机器人框架上的关键字传递参数 - Pass parameter from keyword on robot framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM