暑期实训记录7

flask学习

通过一个含有发布、编辑、删除功能的简易博客的实现熟悉了flask框架,目前在看用户模块的代码,代码整合方面需要跟他们讨论一下
app.py:

from flask import Flask, render_template, request, redirect
from flask_sqlalchemy import SQLAlchemy
from  datetime import datetime

app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///posts.db'
db=SQLAlchemy(app)

class BlogPost(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    title=db.Column(db.String(100),nullable=False)
    content= db.Column(db.Text,nullable=False)
    author=db.Column(db.String(20),nullable=False,default='N/A')
    data_posted=db.Column(db.DateTime,nullable=False,default=datetime.utcnow)

    def __repr__(self):
        return 'Blog post' + str(self.id)

# all_posts = [
#     {
#         'title': 'Post 1',
#         'content': 'This is the content of Post1.',
#         'author': 'GJ'
#     },
#     {
#         'title':'Post 2',
#         'content':'This is the content of Post2.'
#     }
# ]
@app.route('/')
@app.route('/home/<int:id>') #在url中输入变量
def hello(id):
    # return "Hello, " + str(id)
    return render_template('index.html')
def goodbye(id): #多方法无效
    return "Goodbye!" + str(id)

@app.route('/posts',methods=['GET','POST'])
def posts():
    if request.method=='POST':
        post_title=request.form['title']
        post_content=request.form['content']
        post_author=request.form['author']
        new_post=BlogPost(title=post_title,content=post_content,author=post_author)
        db.session.add(new_post)
        db.session.commit()
        return redirect('/posts')
    else:
        all_posts=BlogPost.query.order_by(BlogPost.data_posted).all()
        return render_template('posts.html',posts=all_posts)


@app.route('/onlyget',methods=['GET','POST'])
def get_req():
    return 'You can only get this webpage.'


@app.route('/posts/delete/<int:id>')
def delete(id):
    post = BlogPost.query.get_or_404(id)
    db.session.delete(post)
    db.session.commit()
    return redirect('/posts')


@app.route('/posts/edit/<int:id>',methods=['GET','POST'])
def edit(id):
    post = BlogPost.query.get_or_404(id)
    if request.method=='POST':
        post.title=request.form['title']
        post.author=request.form['author']
        post.content=request.form['content']
        db.session.commit()
        return redirect('/posts')
    else:
        return render_template('edit.html',post=post)

if __name__=="__main__":
    app.run(debug=True)

base.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    <link rel="stylesheet" href="static/css/main.css">-->
    <link rel="stylesheet" href=" {{url_for('static',filename='css/main.css')}}">
    {% block head%} {% endblock %}
</head>
<body>
    {% block body%} {% endblock %}
</body>
</html>

edit.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
<!--    <link rel="stylesheet" href="static/css/main.css">-->
    <link rel="stylesheet" href=" {{url_for('static',filename='css/main.css')}}">
    {% block head%} {% endblock %}
</head>
<body>
    {% block body%} {% endblock %}
</body>
</html>

index.html

{% extends 'base.html' %}

{% block head %}
 <title>Home</title>
{% endblock %}

{% block body%}
<h1>Home Page3</h1>
{% endblock %}

posts.html

{% extends 'base.html' %}

{% block head %}
 <title>Posts</title>
{% endblock %}

{% block body%}
<h1>All Page3</h1>

    <hr>
    <h2>Create new Blog Post:</h2>
    <form action='/posts' method='POST'>
       Title:<input type='text' name='title' id='title'>
       <br>
        Author:<input type='text' name='author' id='author'>
        <br>
        Post:<input type='text' name='content' id='content'>
        <br>
        <input type='submit' value='Post'>
    </form>
    <hr>

    {% for post in posts %}
        <h2>{{ post.title }}</h2>

        {% if post.author %}
            <h3>By:{{ post.author }}</h3>
        {% else %}
            <h3>By: N/A</h3>
        {% endif %}

        <p>{{  post.content  }}</p>
        <a href='/posts/delete/{{post.id}}'>Delete</a>
        <a href='/posts/edit/{{post.id}}'>Edit</a>
        <hr>
    {% endfor %}

{% endblock %}

项目进度

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值