-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathadd.py
More file actions
59 lines (53 loc) · 1.95 KB
/
add.py
File metadata and controls
59 lines (53 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from flask import Flask, render_template,url_for,request,redirect
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='sqlite:///test.db'
db=SQLAlchemy(app)
class Asunto(db.Model):
id=db.Column(db.Integer,primary_key=True)
asunto=db.Column(db.String(200),nullable=False)
email=db.Column(db.String(200),nullable=False)
def __repr__(self):
return '<Asunto %r>' % self.id
@app.route('/',methods=['POST','GET'])
def index():
if request.method=='POST':
Asunto_asunto=request.form['asunto']
Asunto_email=request.form['email']
new_Asunto=Asunto(asunto=Asunto_asunto,email=Asunto_email)
try:
db.session.add(new_Asunto)
db.session.commit()
return redirect('/')
except:
return 'Hubo un error'
else:
asuntos=Asunto.query.order_by(Asunto.id).all()
return render_template('Index.html', asuntos=asuntos)
@app.route('/delete/<int:id>')
def delete(id):
asuntotodelete=Asunto.query.get_or_404(id)
try:
db.session.delete(asuntotodelete)
db.session.commit()
return redirect('/')
except:
return 'Ha ocurrido un error al eliminar'
@app.route('/update/<int:id>',methods=['GET','POST'])
def update(id):
#This name is going to be useful for the update action we have to use the name that we put below
asunto=Asunto.query.get_or_404(id)
if request.method=='POST':
#Using the name that is up for update the informaction with sqlalchemy
asunto.asunto=request.form['asunto']
asunto.email=request.form['email']
try:
db.session.commit()
#The information is update in this moment and we return to the index
return redirect('/')
except:
return 'Hubo un problema'
else:
return render_template('update.html',asunto=asunto)
if __name__ == "__main__":
app.run(debug=True)