新手在处理非常简单的Python / HTML例程时遇到了困难。

huangapple go评论84阅读模式
英文:

Newby struggling with very simple Python / HTML routine

问题

请温柔一点。我正在尝试学习一些Python/Flask和HTML知识,对于一个非常简单的练习,我正在尝试编写一个程序:

  1. 在本地打开一个网页浏览器。
  2. 提示用户选择本地图片。
  3. 在网页上显示该图片。

这些都是本地操作 - 最终我希望将其迁移到云端,但现在我只是想让它在本地工作。

然而,当我的程序执行时,我没有得到完整的图片 - 只有一个通用的"上传的照片"图标。

上传界面:

新手在处理非常简单的Python / HTML例程时遇到了困难。

结果:

新手在处理非常简单的Python / HTML例程时遇到了困难。

以下是我的代码:

FlyFinderWeb.py:

from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import os

app = Flask(__name__)
UPLOAD_FOLDER = 'uploads'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

@app.route('/', methods=['GET', 'POST'])
def upload_photo():
    if request.method == 'POST':
        if 'photo' not in request.files:
            return redirect(request.url)
        
        photo = request.files['photo']
        
        if photo.filename == '':
            return redirect(request.url)
        
        if photo:
            filename = os.path.join(app.config['UPLOAD_FOLDER'], photo.filename)
            photo.save(filename)
            return redirect(url_for('display_photo', filename=photo.filename))
    
    return render_template('upload.html')


@app.route('/display/<filename>')
def display_photo(filename):
    print(os.path.join(app.config['UPLOAD_FOLDER'], filename))  
    return render_template('display.html', filename=filename)

app.run(debug=True, use_reloader=True) #automatically restart server if changes are detected  

if __name__ == '__main__':
    app.run(debug=True)

Upload.html

<!doctype html>
<html>
<head>
    <title>Upload and Display Photo</title>
</head>
<body>
    <h1>Upload a Photo Please 3.0</h1>
    <form method="POST" enctype="multipart/form-data">
        <input type="file" name="photo" accept=".jpg, .jpeg, .png" title="Select File">
        <input type="submit" value="Upload">
    </form>
</body>
</html>

Display.html

<!doctype html>
<html>
<head>
    <title>Display Photo</title>
    <style>
        .image-container {
            text-align: center;
            margin-bottom:;
        }
        .image-container img {
            max-width: 100%;
            max-height: 400px;
        }
    </style>
</head>
<body>
    <h1>Uploaded Photo</h1>

    <div class="image-container">
      <img src="{{ url_for('display_photo', filename=filename) }}" alt="Uploaded Photo">

    </div>
    
    <p>Would you like to repeat another?</p>
    <a href="{{ url_for('upload_photo') }}">Upload Another</a>
</body>
</html>

这是目录结构:

新手在处理非常简单的Python / HTML例程时遇到了困难。

英文:

please be gentle. I'm trying to learn some Python / Flask and HTML and for a very simple exercise I'm trying to write a routine that:

Opens a web browser locally.
Prompts the user to select a local image
Displays the image on the web page.

It's all local stuff - eventually I'd like to move this into the cloud but right now I'm just trying to get it to work locally.

However when my program executes I am not getting a full image - only a generic "uploaded Photo" icon.

The upload screen:

新手在处理非常简单的Python / HTML例程时遇到了困难。

The result:

新手在处理非常简单的Python / HTML例程时遇到了困难。

Here is my code:

FlyFinderWeb.py:


from flask import Flask, render_template, request, redirect, url_for, send_from_directory
import os

app = Flask(__name__)
UPLOAD_FOLDER = &#39;uploads&#39;
app.config[&#39;UPLOAD_FOLDER&#39;] = UPLOAD_FOLDER

@app.route(&#39;/&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])
def upload_photo():
    if request.method == &#39;POST&#39;:
        if &#39;photo&#39; not in request.files:
            return redirect(request.url)
        
        photo = request.files[&#39;photo&#39;]
        
        if photo.filename == &#39;&#39;:
            return redirect(request.url)
        
        if photo:
            filename = os.path.join(app.config[&#39;UPLOAD_FOLDER&#39;], photo.filename)
            photo.save(filename)
            return redirect(url_for(&#39;display_photo&#39;, filename=photo.filename))
    
    return render_template(&#39;upload.html&#39;)


@app.route(&#39;/display/&lt;filename&gt;&#39;)
def display_photo(filename):
    print(os.path.join(app.config[&#39;UPLOAD_FOLDER&#39;], filename))  
    return render_template(&#39;display.html&#39;, filename=filename)



app.run(debug=True, use_reloader=True) #automatically restart server if changes are detected  


if __name__ == &#39;__main__&#39;:
    app.run(debug=True)

Upload.html

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Upload and Display Photo&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Upload a Photo Please 3.0&lt;/h1&gt;
    &lt;form method=&quot;POST&quot; enctype=&quot;multipart/form-data&quot;&gt;
        &lt;input type=&quot;file&quot; name=&quot;photo&quot; accept=&quot;.jpg, .jpeg, .png&quot; title=&quot;Select File&quot;&gt;
        &lt;input type=&quot;submit&quot; value=&quot;Upload&quot;&gt;
    &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Display.html

&lt;!doctype html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Display Photo&lt;/title&gt;
    &lt;style&gt;
        .image-container {
            text-align: center;
            margin-bottom:;
        }
        .image-container img {
            max-width: 100%;
            max-height: 400px;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;h1&gt;Uploaded Photo&lt;/h1&gt;

    &lt;div class=&quot;image-container&quot;&gt;
      &lt;img src=&quot;{{ url_for(&#39;display_photo&#39;, filename=filename) }}&quot; alt=&quot;Uploaded Photo&quot;&gt;

    &lt;/div&gt;
    
    &lt;p&gt;Would you like to repeat another?&lt;/p&gt;
    &lt;a href=&quot;{{ url_for(&#39;upload_photo&#39;) }}&quot;&gt;Upload Another&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;

here is the directory structure:

新手在处理非常简单的Python / HTML例程时遇到了困难。

答案1

得分: 0

你已成功上传了照片。然而,在查看时,你应该区分文件名和图像数据。客户端无法直接使用文件名访问图像,而是需要图像数据来显示图像,服务器可以使用文件名发送图像数据。

为了提供图像数据,你需要另一个端点。

@app.route('/download/<path:filename>')
def download_photo(filename):
	return send_from_directory(
		os.path.abspath(app.config['UPLOAD_FOLDER']), 
		filename
	)

然后需要在display.html文件中更改img标签的src属性的URL。

<div class="image-container">
  <img src="{{ url_for('download_photo', filename=filename) }}" alt="Uploaded Photo">
</div>
英文:

You have successfully managed to upload the photo. When viewing, however, you should distinguish between the file name and the image data. The client cannot access the image directly using the name, but needs the image data to display the image, which can be sent by the server using the file name.

To serve the image data you need another endpoint.

@app.route(&#39;/download/&lt;path:filename&gt;&#39;)
def download_photo(filename):
	return send_from_directory(
		os.path.abspath(app.config[&#39;UPLOAD_FOLDER&#39;]), 
		filename
	)

Then it is necessary to change the URL for the src attribute of the img tag in the display.html file.

&lt;div class=&quot;image-container&quot;&gt;
  &lt;img src=&quot;{{ url_for(&#39;download_photo&#39;, filename=filename) }}&quot; alt=&quot;Uploaded Photo&quot;&gt;
&lt;/div&gt;

huangapple
  • 本文由 发表于 2023年8月9日 02:30:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定