类型错误:loadshortlink() 为参数“shortlink”获得了多个值

错误:类型错误:loadshortlink() 为参数“shortlink”获得了多个值


我的 urls.py:


path('s/<str:shortlink>',views.loadshortlink, name="get_longlink")

视图.py:


def loadshortlink(shortlink):

    print("Translating short link %s" % shortlink)

    link = get_longlink(shortlink)

    return render(request, 'shortlinks/openlong.html', {

        'link': link


    })


def get_longlink(shortlink):

    print('Short link is %s' % shortlink)

    links = Links.objects.filter(shortlink=shortlink)

    if len(links)>1 or len(links)==1:

        link = links[0].longlink

        return link

    else:

        return 'No matched long links'

当我访问网址时:http : //127.0.0.1 : 8000/s/4nI


我收到错误:


Internal Server Error: /s/4nI

Traceback (most recent call last):

File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner

    response = get_response(request)

File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response

    response = self.process_exception_by_middleware(e, request)

File "/home/joel/.local/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response

    response = wrapped_callback(request, *callback_args, **callback_kwargs)

TypeError: loadshortlink() got multiple values for argument 'shortlink'

为什么会这样?


斯蒂芬大帝
浏览 186回答 2
2回答

千万里不及你

视图函数的第一个参数应该是请求。您需要将其添加到loadshortlink:def loadshortlink(request, shortlink):&nbsp; &nbsp; print("Translating short link %s" % shortlink)&nbsp; &nbsp; link = get_longlink(shortlink)&nbsp; &nbsp; return render(request, 'shortlinks/openlong.html', {&nbsp; &nbsp; &nbsp; &nbsp; 'link': link&nbsp; &nbsp; })

慕桂英3389331

实际上,它无法处理请求,因为 loadshortlink 方法缺少请求参数。你的代码应该是:def loadshortlink(request, shortlink):&nbsp; &nbsp; print("Translating short link %s" % shortlink)&nbsp; &nbsp; link = get_longlink(shortlink)&nbsp; &nbsp; return render(request, 'shortlinks/openlong.html', {&nbsp; &nbsp; &nbsp; &nbsp; 'link': link&nbsp; &nbsp; })def get_longlink(shortlink):&nbsp; &nbsp; print('Short link is %s' % shortlink)&nbsp; &nbsp; links = Links.objects.filter(shortlink=shortlink)&nbsp; &nbsp; if len(links)>1 or len(links)==1:&nbsp; &nbsp; &nbsp; &nbsp; link = links[0].longlink&nbsp; &nbsp; &nbsp; &nbsp; return link&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; return 'No matched long links'
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python