Flask从入门到做出一个博客的大型教程(五)

Flask从入门到做出一个博客的大型教程(五)

在开始之前,先来看下项目的整体结构。

flask/
├── app
│   ├── forms.py
│   ├── __init__.py
│   ├── models.py
│   ├── routes.py
│   └── templates
│       ├── base.html
│       ├── index.html
│       ├── login.html
│       ├── register.html
│       └── test.html
├── config.py
├── migrations
│   ├── alembic.ini
│   ├── env.py
│   ├── README
│   ├── script.py.mako
│   └── versions
│       ├── 063e1979f08c_questions.py
├── question.py

6 用户个人资料页

登录模块完成了,接下来做一做用户个人资料页的显示。

app/routes.py : 添加用户方法

@app.route('/user/<username>')
@login_required
def user(username):

    user = User.query.filter_by(username=username).first_or_404()
    posts = [
        {
  
  'author':user,'body':'测试Post #1号'},
        {
  
  'author':user,'body':'测试Post #2号'}
    ]

    return render_template('user.html',user=user,posts=posts)

既然添加了方法,肯定要添加对应的模板了。

app/templates/user.html : 用户个人中心模板

{% extends "base.html" %}

{% block content %}
    <h1>用户: {
  
  { user.username }}</h1>
    <hr>
    {% for post in posts %}
    <p>
    {
  
  { post.author.username }} 说: <b>{
  
  { post.body }}</b>
    </p>
    {% endfor %}
{% endblock %}

对基类模板进行小小的改动,当用户是未登录状态看不到用户中心的超链接,登录后看到。这会很有感意思。

app/templates/base.html : 完善基类模板

  <div>博客 :
              <a href="{
   
   { url_for('index') }}">首页</a>
      {% if current_user.is_anonymous %}
      <a href="{
   
   { url_for('login') }}">登录<
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值