为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

开满天机 2023-04-11 16:11:18
我在本地计算机上运行 python-django 服务器使用 django.urls 我尝试定义一个包含文件夹和文件的服务器路径我的代码:urls.py:from django.urls import pathurlpatterns = [    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/ddimageSaved() got an unexpected keyword argument 'folder'   回溯是:Environment:Request Method: GETRequest URL: http://localhost:5000/imageSaved/dd/ddDjango Version: 3.1rc1Python Version: 3.8.5Installed 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/ddException Value: imageSaved() got an unexpected keyword argument 'folder'谢谢!!
查看完整描述

1 回答

?
芜湖不芜

TA贡献1796条经验 获得超7个赞

错误很明显:您不支持 url 参数。

视图函数将带有可选 URL 部分的请求转换为响应,因此它的签名是:

def view_function(request, *args, **kwargs):
    ...

在你的情况下:

def imageSaved(request, folder, picture):
    res=request.__str__()  # This isn't useful, but probably for debugging
    return HttpResponse(res)


查看完整回答
反对 回复 2023-04-11
  • 1 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信