Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Flask应用程序无法识别动态创建的目录_Python_Python 3.x_Flask_Uuid_Glob - Fatal编程技术网

Python Flask应用程序无法识别动态创建的目录

Python Flask应用程序无法识别动态创建的目录,python,python-3.x,flask,uuid,glob,Python,Python 3.x,Flask,Uuid,Glob,我正在尝试创建一个Flask应用程序,其中一些功能包括用户能够将图像上传到静态文件夹中自己的目录中 我基于我的代码,事实上它基本上是相同的(除了AJAX功能),所以我真的看不出哪里出了问题(我在自己的环境中运行了kirsle的应用程序,它工作得很好) 当我访问/static/uploads/{uuid\u goes\u here}/img.jpg时,我可以看到图像-显然它正在被上传。但是,访问/files/{uuid\u goes\u here}会导致执行如果不是os.path.isdir(lo

我正在尝试创建一个Flask应用程序,其中一些功能包括用户能够将图像上传到静态文件夹中自己的目录中

我基于我的代码,事实上它基本上是相同的(除了AJAX功能),所以我真的看不出哪里出了问题(我在自己的环境中运行了kirsle的应用程序,它工作得很好)

当我访问
/static/uploads/{uuid\u goes\u here}/img.jpg
时,我可以看到图像-显然它正在被上传。但是,访问
/files/{uuid\u goes\u here}
会导致执行
如果不是os.path.isdir(location)

当我注释掉这段代码并尝试直接转到complete.html时,
{%for file in files%}
似乎没有运行,因为没有任何图像出现

我的代码是:

app.py

# Route that will process the file upload
@app.route('/upload', methods=['POST'])
def upload():
    form = request.form
    username = current_user.username #for later when I replace uuid's with usernames
    uuid_key = str(uuid4())
    print("Session Key: {}".format(uuid_key))

    target = "static/uploads/{}".format(uuid_key)
    try:
        os.mkdir(target)
    except FileExistsError:
        return "Couldn't create directory {}".format(target)

    for upload in request.files.getlist("file"):
        filename = upload.filename.rsplit("/")[0]
        destination = '/'.join([target, filename])
        print( "Accepting: {}\n and saving to: {}".format(filename, destination))
        upload.save(destination)

    return redirect(url_for('complete', uuid_key=uuid_key))

@app.route("/files/<uuid_key>")
def complete(uuid_key):
    location = "/static/uploads/{}".format(uuid_key)
    if not os.path.isdir(location):
        return "Error! {} not found!".format(location)

    files = []
    for file in glob.glob("{}/*.*".format(location)):
        fname = file.split(os.sep)[-1]
        files.append(fname)

    return render_template('complete.html', uuid=uuid_key, files=files)
#将处理文件上载的路由
@app.route('/upload',methods=['POST'])
def upload():
form=request.form
username=current_user.username#稍后我用用户名替换uuid时使用
uuid_key=str(uuid4())
打印(“会话键:{}”。格式(uuid_键))
target=“static/uploads/{}”。格式(uuid_键)
尝试:
os.mkdir(目标)
除文件ExistError外:
返回“无法创建目录{}”。格式(目标)
用于在request.files.getlist(“文件”)中上载:
filename=upload.filename.rsplit(“/”[0]
destination='/'.join([target,filename])
打印(“接受:{}\n并保存到:{}”。格式(文件名,目标))
上传.保存(目的地)
返回重定向(url_for('complete',uuid_key=uuid_key))
@app.route(“/files/”)
def完成(uuid_键):
location=“/static/uploads/{}”。格式(uuid_键)
如果不是os.path.isdir(位置):
返回“错误!{}未找到!”。格式(位置)
文件=[]
对于glob.glob(“{}/*.*.format(location))格式的文件:
fname=file.split(os.sep)[-1]
files.append(fname)
返回呈现模板('complete.html',uuid=uuid\u键,files=files)
complete.html

{% extends 'layout.html' %}

{% block content %}

{% for file in files %}
    <h2>{{ file }}</h2>
    <img src="{{ url_for('static', filename='/uploads/{}/{}'.format(uuid, file)) }}">
{% endfor %}

{% endblock %}
{% extends 'layout.html' %}

{% block content %}

    <form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file" accept="image/*"><br /><br />
        <input type="submit" value="Upload">
      </form>

{% endblock %}
{%extends'layout.html%}
{%block content%}
{文件%中的文件为%s}
{{file}}
{%endfor%}
{%endblock%}
post.html

{% extends 'layout.html' %}

{% block content %}

{% for file in files %}
    <h2>{{ file }}</h2>
    <img src="{{ url_for('static', filename='/uploads/{}/{}'.format(uuid, file)) }}">
{% endfor %}

{% endblock %}
{% extends 'layout.html' %}

{% block content %}

    <form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="file" accept="image/*"><br /><br />
        <input type="submit" value="Upload">
      </form>

{% endblock %}
{%extends'layout.html%}
{%block content%}


{%endblock%}

我将我的代码与kirsle的代码进行了比较,我看不出哪里出了问题。

您在应该是相对路径的前面放了一个
/

location = "static/uploads/{}".format(uuid_key)
    if not os.path.isdir(location):
假设您的文件结构为:

app.py
static/
templates/

这个逻辑在app.py中

奇怪的是,你的根路径上会有一个静态目录?“/static”是完整路径,与当前文件无关。请尝试location=“static/uploads/{}”.format(uuid_key),只要它在简单的app.py文件中运行,其中static是相对于app.py文件的!哈哈,太尴尬了。。。这就像在冰箱里寻找黄油——我从来没有见过这些东西。如果答案有助于你解决问题,请确保你接受它:)是的——它不会让你在5分钟内接受……奇怪。