#python/flask error
1 messages · Page 1 of 1 (latest)
- Go post the code
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)
Would probably be useful to know what endpoint you're requesting and how you make that request
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
Check your browser network tab?
Which route is it POSTing to?
/login
I wonder why your GET is 404ing then
scheme
http
host
127.0.0.1:5000
filename
/login
this is what it says
whats going wrong
Should it be just login instead of /login or something? Haven't touched Flask in years
in my html form in only need to say login not /login
right?
or do i need to remove the / in the flask code
This could be it but I really don't know
Can you try sending the request from another program (e.g. Postman, Bruno, etc:)?
i gonna try
That way you could isolate where the issue is coming from (Flask or frontend)
without / i got the same
i never used that but i gonna try
its hosted locally thats be working with the program?
ye?
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
Nice!
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?
Probably /close