django python server- error: imageSaved() 得到了一个意外的

我在本地计算机上运行 python-django 服务器


使用 django.urls 我尝试定义一个包含文件夹和文件的服务器路径


我的代码:


urls.py:


from django.urls import path

urlpatterns = [


    path("", server.views.index, name="index"),

    path("imageSaved/<str:folder>/<str:picture>", server.views.imageSaved, name="imageSaved")

]

我也试试:


 path("imageSaved/<path:path>", server.views.imageSaved, name="imageSaved")

我得到了同样的错误。


imageSaved 功能来自 server.views:


def imageSaved(request):

    res=request.__str__()

    return HttpResponse(res)

因为路径是一个字符串,所以我这样定义它:<str:folder>/<str:picture>


当我写一个网址时:


http://localhost:5000/imageSaved/fff/ddd

我收到错误:


TypeError at /imageSaved/dd/dd

imageSaved() got an unexpected keyword argument 'folder'

   

回溯是:


Environment:



Request Method: GET

Request URL: http://localhost:5000/imageSaved/dd/dd


Django Version: 3.1rc1

Python Version: 3.8.5

Installed Applications:

['django.contrib.admin',

 'django.contrib.auth',

 'django.contrib.contenttypes',

 'django.contrib.sessions',

 'django.contrib.messages',

 'django.contrib.staticfiles',

 'server']

Installed Middleware:

('whitenoise.middleware.WhiteNoiseMiddleware',

 'django.middleware.security.SecurityMiddleware',

 'django.contrib.sessions.middleware.SessionMiddleware',

 'django.middleware.common.CommonMiddleware',

 'django.middleware.csrf.CsrfViewMiddleware',

 'django.contrib.auth.middleware.AuthenticationMiddleware',

 'django.contrib.messages.middleware.MessageMiddleware',

 'django.middleware.clickjacking.XFrameOptionsMiddleware')




Traceback (most recent call last):

  File "Local\Programs\Python\Python38\lib\site-packages\django-3.1rc1-py3.8.egg\django\core\handlers\exception.py", line 47, in inner

    response = get_response(request)

  File "Local\Programs\Python\Python38\lib\site-packages\django-3.1rc1-py3.8.egg\django\core\handlers\base.py", line 179, in _get_response

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


Exception Type: TypeError at /imageSaved/dd/dd

Exception Value: imageSaved() got an unexpected keyword argument 'folder'

谢谢!!


开满天机
浏览 120回答 1
1回答

芜湖不芜

错误很明显:您不支持 url 参数。视图函数将带有可选 URL 部分的请求转换为响应,因此它的签名是:def&nbsp;view_function(request,&nbsp;*args,&nbsp;**kwargs): &nbsp;&nbsp;&nbsp;&nbsp;...在你的情况下:def&nbsp;imageSaved(request,&nbsp;folder,&nbsp;picture): &nbsp;&nbsp;&nbsp;&nbsp;res=request.__str__()&nbsp;&nbsp;#&nbsp;This&nbsp;isn't&nbsp;useful,&nbsp;but&nbsp;probably&nbsp;for&nbsp;debugging &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;HttpResponse(res)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python