-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (48 loc) · 1.69 KB
/
main.py
File metadata and controls
58 lines (48 loc) · 1.69 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
from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///post.db'
db = SQLAlchemy(app)
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False)
intro = db.Column(db.String(300), nullable=False)
text = db.Column(db.Text, nullable=False)
date = db.Column(db.DateTime, default=datetime.utcnow)
def __repr__(self):
return '<Article %r>' % self.id
@app.route('/')
def index():
return render_template('index.html')
@app.route('/coding')
def coding():
return render_template('coding.html')
@app.route('/news')
def news():
articles = Article.query.order_by(Article.date.desc()).all()
return render_template('news.html', articles=articles)
@app.route('/news/<int:id>')
def news_det(id):
article = Article.query.get(id)
return render_template('news_det.html', article=article)
@app.route('/add_article', methods=['POST', 'GET'])
def add_article():
if request.method == 'POST':
title = request.form['title']
intro = request.form['intro']
text = request.form['text']
article = Article(title=title, intro=intro,text=text)
try:
db.session.add(article)
db.session.commit()
return redirect('/news')
except:
print('Что-то пошло не так')
else:
return render_template('add_article.html')
if __name__ == "__main__":
with app.app_context():
db.create_all()
app.run(debug=True)