scutomix
scutomix

Reputation: 33

Flask bad request error ( Bad Request The browser (or proxy) sent a request that this server could not understand.)

Hi i coding a blog script but get 400 Bad Request error. Python code:

@app.route('/panel',methods=['GET','POST'])
def panel():
    
    if 'user_id' in session:
        if request.method == "POST":
            title = request.form['title']
            context = request.form['text']
            img = request.files['img']
            author = request.form['author']
            

            new = Posts(title=title,context=context,image=img.read(),author=author)
            db.session.add(new)
            db.session.commit()
            return redirect(url_for('blog'))




    else:
        return redirect(url_for("admin"))

    return render_template('panel.html')

Html code:

<form action="" method="POST" class="form">
   
    <ul>
        <li>
            <label for="title">Başlık</label>
           <input name="title" id="title"/>
        </li>
        <li>
            <label for="text">İçerik</label>
           <input name="text" id="text">
        </li>
        <li>
            <label for="img">Fotoğraf</label>
           <input name="img" id="img" type="file" />
        </li>
        <li>
            <label for="author">Yazar</label>
           <input name="author" id="author"/>
        </li>
        <li>
            <button type="submit">PAYLAS</button>
        </li>
    </ul>
</form>

I have never received such an error before, I don't know why. I guess there might be errors in the forms.

Please help me

Upvotes: 0

Views: 1301

Answers (2)

Er. Sachish
Er. Sachish

Reputation: 439

Change this line in Html Form. It should work.

<form action="{{ url_for('panel') }}" method="post" class="form">

Upvotes: 1

str4nger
str4nger

Reputation: 28

If you are trying to upload a file your form tag should contain attribute enctype="multipart/form-data". example:

<form action="upload.php" method="post" enctype="multipart/form-data">

Upvotes: 0

Related Questions