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
|
from blog.model.User import User from blog.model.Category import Category import os
from blog import app,db from flask import request,render_template,flash,abort,url_for,redirect,session,Flask,g
@app.route('/') def show_entries(): categorys = Category.query.all() return render_template('show_entries.html',entries=categorys)
@app.route('/add',methods=['POST']) def add_entry(): if not session.get('logged_in'): abort(401) title = request.form['title'] content = request.form['text'] category = Category(title, content) db.session.add(category) db.session.commit() flash('New entry was successfully posted') return redirect(url_for('show_entries'))
@app.route('/login',methods=['GET','POST']) def login(): error = None if request.method == 'POST': username = request.form['username'] password = request.form['password'] user = User.query.filter_by(username=request.form['username']).first() passwd = User.query.filter_by(password = request.form['password']).first()
if user is None: error = 'Invalid username' elif password is None: error = 'Invalid password' else: session['logged_in'] = True flash('You were logged in') return redirect(url_for('show_entries')) return render_template('login.html', error=error)
@app.route('/logout') def logout(): session.pop('logged_in', None) flash('You were logged out') return redirect(url_for('show_entries'))
|