Issue
This is the code of my flask app:
from datetime import datetime
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:@localhost/codingbreaker'
db = SQLAlchemy(app)
class Contacts(db.Model):
sno = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)
phone_num = db.Column(db.String(12), nullable=False)
msg = db.Column(db.String(120), nullable=False)
date = db.Column(db.String(12), nullable=True)
email = db.Column(db.String(20), nullable=False)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/about")
def about():
return render_template("about.html")
@app.route("/post")
def post():
return render_template("post.html")
@app.route("/contact", methods = ['GET', 'POST'])
def contact():
if (request.method == 'POST'):
'''Add entry to the database'''
name = request.form.get('name')
email = request.form.get('email')
phone = request.form.get('phone')
message = request.form.get('message')
entry = Contacts(name=name, phone_num=phone, msg=message, email=email, date=datetime.now())
db.session.add(entry)
db.session.commit()
return render_template("contact.html")
app.run(debug = True)
I'm getting this error always as shown below after submitting my contact form:
It is always saying that column 'name' cannot be null and I have done the same everywhere but also it is saying the same.
This is my PHPMyAdmin database's table pic:
This is the contact form where I enter the details:
It is giving me this kind of error every time then I decided to convert all the tables's elements of my PhpMyAdmin to null and then I submitted the form and the form is submitted also but when I had gone to my PhpMyAdmin database all the element's values were NULL.
Solution
The error was not in my flask app but it was in the HTML file because the (id=x) and other things, I have written somewhere else and it has to be written in the correct format so you can get the correct process.
Answered By - Adityaraj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.