405烧瓶中不允许使用方法

时间:2020-03-24 01:26:50

标签: python forms flask flask-login flask-admin

因此,我正在创建一个用于文件上传的登录门户,包括每个会话的会话处理,为用户创建一个文件夹并将文件存储在该文件夹中。 我面临405(我尝试上传文件时,方法不允许错误)

from flask import Flask, redirect, url_for, render_template, request, session,flash
from datetime import timedelta
import subprocess
from werkzeug.utils import secure_filename
import os
import datetime
import urllib.request

app = Flask(__name__,template_folder="Some Template Directory")

app.permanent_session_lifetime = timedelta(minutes=5)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]iasdfffsd/'
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'csv'])

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


@app.route('/')
def home(): 
    return render_template('try.html')


@app.route('/login' , methods = ["POST", "GET"])
def login():
    if request.method == "POST":
        session.permanent = True
        user = request.form["name"]
        session["abc"] = user
        parent_dir = "Directory path for creating sub dir for each session"
        global path1
        path1= os.path.join(parent_dir,user)
        os.makedirs(path1)
        return redirect(url_for("user"))


    return render_template('login.html')


@app.route('/user' )
def user():
    if "pawan" in session:
        user = session["abc"]
        return f"<h1>{user}</h1>"'<br>' + \
                "<b><a href = '/upload'> Click here to uploads the files </a></b>"
    else:
         return redirect(url_for("login"))


@app.route('/upload')
def upload_form():
    return render_template('sessionupload.html')

@app.route('/upload', methods=['POST','GET'])
def upload_file():
    if request.method == 'POST':
         if request.files:
            files = request.files.getlist('files[]')
            if files.filename == "":
                flash("No file")
                return redirect(request.url)
            if allowed_file(files.filename):
                abcd = secure_filename(files.filename)
                files.save(os.path.join(path1, abcd))
                flash("file saved")
                return redirect(request.url)
    render_template('sessionupload.html')



@app.route("/logout")
def logout():   
    session.pop("abc", None)#pops out the key value of the session , if the is not present then it will print none
    return redirect(url_for("login"))


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

sessionupload.html:

<!doctype html>
<title>Upload FIles</title>
<h2>Select file(s) to upload</h2>
<p>
    {% with messages = get_flashed_messages() %}
      {% if messages %}
        <ul class=flashes>
        {% for message in messages %}
          <li>{{ message }}</li>
        {% endfor %}
        </ul>
      {% endif %}
    {% endwith %}
</p>
<form method="post" action="/" enctype="multipart/form-data">
    <dl>
        <p>
            <input type="file" name="files[]" multiple="true" autocomplete="off" required>
        </p>
    </dl>
    <p>
        <input type="submit" value="Submit">
    </p>
</form>

我无法弄清楚为什么会发生错误,我甚至在上传URL的路径中包含GET和POST吗? 任何建议将不胜感激。我的截止日期临近:)

0 个答案:

没有答案