#python/flask error

1 messages · Page 1 of 1 (latest)

buoyant crown
#

Method Not Allowed

The method is not allowed for the requested URL.

i got this

#

i can send the python code that i use

#

can some one help me to fiks it?

rocky talon
#
  • Go post the code
buoyant crown
#

import csv
import os

from flask import Flask, render_template, redirect, url_for, session, request

CSV_FILE = 'users.csv'

def append_to_csv(username, title, secret):
with open(CSV_FILE, 'a', newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow([username, title, secret])

def read_from_csv(username):
secrets = []
if os.path.exists(CSV_FILE):
with open(CSV_FILE, 'r') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
if row[0] == username:
secrets.append(row[1:])
return secrets

def read_users():
if os.path.exists(CSV_FILE):
with open(CSV_FILE, 'r') as file:
csv_reader = csv.DictReader(file)
users = {row['username']: {'password': row['password']} for row in csv_reader}
else:
users = {}
return users

def write_user(username, password):
if not os.path.exists(CSV_FILE):
with open(CSV_FILE, mode='w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerow(["username", "password"])

with open(CSV_FILE, mode='a', newline='') as file:
    csv_writer = csv.writer(file)
    csv_writer.writerow([username, password])

app = Flask(name)
app.secret_key = 'your_secret_key'

users = read_users()

@app.route('/')
def index():
username = session.get('username')
return render_template('index.html', username=username)

@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# inloggen
username = request.form['username']
password = request.form["password"]
if username in users and users[username]['password'] == password:
session['username'] = username
return redirect(url_for('secrets'))
else:
return 'Login Unsuccessful. Please check username and password.'
else:
return render_template('login.html')

@app.route('/registratie', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
# registeren
username = request.form['username']
password = request.form["password"]

    if username not in users:
        write_user(username, password) 
        users[username] = {'password': password}
        session['username'] = username
        return redirect(url_for('secrets'))
    else:
        return 'Registration Unsuccessful. Please choose a different username.'
else:
    return render_template('registratie.html')

@app.route('/secrets')
def secrets():
username = session.get('username')
if username:
secrets = read_from_csv(username)
return render_template('secrets.html', username=username, secrets=secrets)
else:
return redirect(url_for('login'))

if name == 'main':
app.run(debug=True)

(this is the code)

rocky talon
#

Would probably be useful to know what endpoint you're requesting and how you make that request

buoyant crown
#

i try to when i fill something in my html login form (that are in the datebase) then i go a page for only people with a account

rocky talon
#

Check your browser network tab?

buoyant crown
#

i am now in the network tab

#

i see post is 405 and get is 404

rocky talon
#

Which route is it POSTing to?

buoyant crown
#

/login

rocky talon
#

I wonder why your GET is 404ing then

buoyant crown
#

scheme
http
host
127.0.0.1:5000
filename
/login

this is what it says

buoyant crown
rocky talon
#

Should it be just login instead of /login or something? Haven't touched Flask in years

buoyant crown
#

in my html form in only need to say login not /login

#

right?

#

or do i need to remove the / in the flask code

rocky talon
#

Can you try sending the request from another program (e.g. Postman, Bruno, etc:)?

buoyant crown
#

i gonna try

rocky talon
#

That way you could isolate where the issue is coming from (Flask or frontend)

buoyant crown
#

without / i got the same

buoyant crown
#

its hosted locally thats be working with the program?

rocky talon
#

ye?

buoyant crown
#

its works

#

<form action="{{ url_for('home.login') }}" method="post">

i add this in html and change some code in python and i can login

rocky talon
#

Nice!

buoyant crown
#

but i only to figer out why he is not using my csv file that is allready with data he is now creating a new one but thnx for the helpp

#

that also working it was User not user thnx for alll the help

#

Can is close this topic?

rocky talon
#

Probably /close