示例#1
0
def book():
    if request.method == 'GET':
        return render_template('book.html')
    else:
        book_name = request.form.get('book_name')
        book_type = request.form.get('book_type')
        fee = request.form.get('fee')
        content = request.form.get('content')
        book_img = request.files.get('book_img')
        if book_name and fee and content and book_img and book_type:
            user_id = session.get('user_id')
            user = User.query.filter(User.id == user_id).first()
            book = Book(book_name=book_name,book_type=book_type, 
                            fee=fee,content=content)  #生成新对象
            book_image = user.user_name + '_' + book_name + '.jpg' #图书照片图片
            book.book_image = book_image
            UPLOAD_FOLDER = r'C:\mygit\book_back\app\static\images'
            book_img.save(os.path.join( UPLOAD_FOLDER , book_image)) 
            book.user = user
            db.session.add(book)
            db.session.commit()
            return redirect(url_for('index'))
        else :
            flash('填写不完整,请重新填写')
            return redirect(url_for('book'))