简体   繁体   English

得到了一个意外的关键字参数“pzn”

[英]Got an unexpected keyword argument 'pzn'

I'm struggling with following problem:我正在努力解决以下问题:

fachbereich_detailview() got an unexpected keyword argument 'pzn' fachbereich_detailview() 得到了一个意外的关键字参数“pzn”

The error tells me that there is something wrong with my urls.该错误告诉我我的网址有问题。 If I change the last part of the url to int:test, it tells me that the unexpected keyword argument is test.如果我将 url 的最后一部分更改为 int:test,它会告诉我意外的关键字参数是 test。

The query 'product = Products.objects.get(pzn="existingpzn")' is working fine (Tested with shell).查询 'product = Products.objects.get(pzn="existingpzn")' 工作正常(用 shell 测试)。

views.py视图.py

[...]
def fachbereich_detailview(request, pzn):

    context = {}

    try:
        product = Products.objects.get(pzn=pzn)
    except:
        return redirect('fachbereich')

    context['product'] = product

    return render(request, 'app/LoginArea/fachbereich_detailview.html', context)
[...]

urls.py网址.py

[...]
path('Produkt/<int:pzn>/', views.fachbereich_detailview, name='fachbereich_detailview'),
[...]

Html: html:

<a href="{% url 'fachbereich_detailview' product.pzn %}" class="small-text text-underline text-uppercase">Mehr erfahren</a>

I just can't figure out what the problem is.我只是无法弄清楚问题是什么。

It is because you are setting integer in your url, but you are passing string to your view.这是因为您在 url 中设置integer ,但您正在将string传递给您的视图。 Change your url to that:将您的网址更改为:

path('Produkt/<str:pzn>/', ...),

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM