Hello, I’m trying to create a work database for myself. I’m fairly new to this and am piecing it together as I go. I’ve created an HTML form as the origin of the info. On save, the information is sent to a csv file for storage. At the moment, the header is sent every time. I’m having trouble writing this so that the header only prints the 1st time, then subsequently only the values are sent. There’s a lot of information out there, but I can’t pinpoint exactly what I need here.
Also if there are areas to simplify, let me know.
@app.route("/", methods=["POST", "GET"])
def customer_info():
if request.method == "POST":
first_name = request.form['firstname']
last_name = request.form['lastname']
company = request.form['company']
email_address = request.form['email']
phone_number = request.form['phonenumber']
address = request.form['address']
address_2 = request.form['address2']
city = request.form['city']
state = request.form['state']
zip_code = request.form['zipcode']
with open("customer_info.csv", mode="a", newline="") as csvfile:
fieldnames = ["First Name", "Last Name", "Company", "Email Address", "Phone Number", "Address", "Address 2", "City", "State", "ZIP Code"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({
"First Name": first_name,
'Last Name': last_name,
'Company': company,
'Email Address': email_address,
'Phone Number': phone_number,
'Address': address,
'Address 2': address_2,
'City': city,
'State': state,
'ZIP Code': zip_code,
})
return render_template("customer_info.html")