I'm creating an app after creating the database model. It has not given any error but after adding the db.commit()
in index
route it is not showing the index page but showing other pages without any error. I've provided links to the error images. I'm not getting what I did wrong.
app.py
->
from flask import Flask , render_template, request, jsonify
from flask_sqlalchemy import SQLAlchemy
import datetime as dt
from flask_marshmallow import Marshmallow
#app config
app=Flask(__name__)
app.config['SECRET_KEY']="ASGH}|#%*9"
app.config['SQLALCHEMY_DATABASE_URI']="sqlite:///site.db"
#db
db=SQLAlchemy(app)
ma=Marshmallow(app)
#model
class contact(db.Model):
id= db.Column(db.Integer, primary_key="True")
name= db.Column(db.String(30), nullable=False)
mobile= db.Column(db.Integer, unique=True, nullable=False)
email= db.Column(db.String(45), nullable=False)
company_name= db.Column(db.String(30), nullable=False)
subject= db.Column(db.String(45), nullable=False)
message= db.Column(db.Text, nullable=False)
date_posted= db.Column(db.DateTime, nullable=False, default=dt.datetime.now())
class ContactSchema(ma.Schema):
class Meta:
fields=("id","name","mobile","email","company_name","subject","message","date_posted")
cnt_schema= ContactSchema()
cnts_schema= ContactSchema(many=True)
#pages
@app.route("/", methods=['GET', 'POST'])
def index():
if request.method == "POST":
name= request.form.get("Name")
mobile= request.form.get("mobile")
email= request.form.get("email")
cmp_nm= request.form.get("c_name")
sub= request.form.get("subject")
msg= request.form.get("msg")
#storing into db
cnt= contact(name,mobile,email,cmp_nm,sub,msg)
db.session.add(cnt)
db.session.commit()
#alter box required
return render_template("index.html")
templates/index.html
->
{% extends "layout.html" %}
{% block title %} {% endblock %}
{% block body %}
<div class="container md:mx-auto">
<h3>Contact</h3>
<div class="row">
<form class="col s12" action="{{ url_for("app")}}" method="post">
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">account_circle</i>
<input id="icon_prefix" name="Name" type="text" class="validate">
<label for="icon_prefix">Name</label>
</div>
<div class="input-field col s6">
<i class="material-icons prefix">phone</i>
<input id="icon_telephone" name="mobile" type="tel" class="validate">
<label for="icon_telephone">Telephone</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">email</i>
<input id="icon_email" name="email" type="tel" class="validate">
<label for="icon_email">Email</label>
</div>
<div class="input-field col s6">
<i class="material-icons prefix">business</i>
<input id="icon_business" name="c_name" type="tel" class="validate">
<label for="icon_business">Company Name</label>
</div>
</div>
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">mode_edit</i>
<input id="icon_prefix2" name="subject" type="text" class="validate">
<label for="icon_prefix2">Subject</label>
</div>
<div class="input-field col s6">
<i class="material-icons prefix">keyboard</i>
<textarea id="textarea1" name="msg" class="materialize-textarea"></textarea>
<label for="icon_keyboard">Message</label>
</div>
</div>
<button class="btn waves-effect waves-light" type="submit" name="action">Submit
<i class="material-icons right">send</i>
</button>
</form>
</div>
</div>
{% endblock %}
After adding database this is the exception I'm getting: 1, 2.
How can I resolve it?
CodePudding user response:
in templates/index.html
you made a mistake:
<form class="col s12" action="{{ url_for("app")}}" method="post">
should be :
<form class="col s12" action="{{ url_for("index")}}" method="post">