-
- 个人中心—视图函数带标签页面参数tag
@app.route('/usercenter/<user_id>/<tag>')
def usercenter(user_id, tag):
if tag == ‘1':
return render_template('usercenter1.html', **context) - 个人中心—导航标签链接增加tag参数
<li role=“presentation”><a href=“{{ url_for(‘usercenter’,user_id = user.id,tag = ‘1’) }}">全部问答</a></li> - 个人中心—有链接到个人中心页面的url增加tag参数
<a href="{{ url_for('usercenter',user_id = session.get('userid'), tag=1) }}">{{ session.get('user') }}</a> -
@app.route('/usercenter/<user_id>/<tag>') @login_re def usercenter(user_id,tag): user=User.query.filter(User.id==user_id).first() context={ # 'username':user.Username, # 'questions':user.question, # 'comments':user.comments 'user':user } # return render_template('usercenter.html', **context) if tag == '1': return render_template('user1.html', **context) elif tag == '2': return render_template('user2.html', **context) else: return render_template('user3.html', **context)
index.html
-
{% extends 'base.html' %} {% block title %}首页{% endblock %} {% block head %} <link href="{{ url_for('static',filename='css/index.css') }}" rel="stylesheet" type="text/css"> <script src="{{ url_for('static',filename='js/index.js') }}"></script> {% endblock %} {% block main %} <ul class="list-group" style="margin-left:20%;margin-top: 5%;margin-right:20%"> {# <li class="list-group-item">#} {# <span class="glyphicon glyphicon-leaf" aria-hidden="true"></span>#} {# <a href="{{ url_for('detail')}}">username</a>#} {# <br>#} {# <a href="#)"title></a>#} {# <span class="badge">create_time</span>#} {# <p>detauk</p>#} {% for foo in question %} <li class="list-group-item" > <a href={{ url_for('usercenter',user_id=foo.author_id,tag=1 )}}>{{ foo.author.username }} </a><br> <a href={{ url_for('detail',question_id=foo_id)}}>{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <p class="abstract">{{ foo.detail }}</p> </li> {% endfor %} </ul> <div> <div class="img"> <a href="http://www.baidu.cn/"> <img src="http://p3.so.qhimgs1.com/bdr/_240_/t015a62ea10ce841183.jpg"></a> <div align="center"><a href="http://www.baidu.cn/" >阿斯顿马丁</a></div> </div> <div class="img"> <a href="http://www.baidu.cn/"> <img src="http://p2.so.qhimgs1.com/bdr/_240_/t014017698c50318c42.jpg"></a> <div align="center"><a href="http://www.baidu.cn/">雪佛兰</a></div> </div> <div class="img"> <a href="http://www.baidu.cn/"> <img src="http://p5.so.qhimgs1.com/bdr/_240_/t01d01aca9f5db2159b.jpg"></a> <div align="center"><a href="http://www.baidu.cn/">兰博基尼</a></div> </div> <div class="img"> <a href="http://www.baidu.cn/"> <img src="http://p5.so.qhimgs1.com/bdr/_240_/t01fb87f1928a69d5eb.jpg"></a> <div align="center"><a href="http://www.baidu.cn/">奔驰</a></div> </div> </div> {% endblock %}
- userbase.html
{% extends 'base.html' %} {% block title %}个人中心{% endblock %} {% block head %} <style> .nav1 >li{ list-style: none; float: left; margin: 10px; } </style> {% endblock %} {% block main %} <div class="nav1"> <ul> <li role="presentation"><a href="{{ url_for('usercenter',user_id=user.id ,tag='1')}}">全部问答</a></li> <li role="presentation"><a href="{{ url_for('usercenter',user_id=user.id ,tag='2')}}">全部评论</a></li> <li role="presentation"><a href="{{ url_for('usercenter',user_id=user.id ,tag='3')}}">个人资料</a></li> </ul> </div> {% block user %}{% endblock %} {% endblock %}
-
base.html
-
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>{% block title%}car{% endblock %}</title> <link href="{{ url_for('static',filename='css/base.css') }}" rel="stylesheet" type="text/css"> <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="{{ url_for('static',filename='js/base.js') }}"></script> {% block head %} {% endblock %} <style> nav navbar-nav li { list_style: none; float: left; margin: 10px; } .nav1 li{ list-style: none; float: left; margin: 10px; } </style> </head> <body id="mybody"> <nav class="navbar" role="navigation" style=""> <div class="container-fluid" id="container"> <div class="navbar-header"> <a class="navbar-brand" href="{{ url_for('index') }}">首页</a> </div> <div> <ul class="nav navbar-nav"> {% if username %} <li><a href={{ url_for('usercenter',user_id=session.get('userid'),tag=1) }}>{{ username }}</a></li> <li><a href="{{ url_for('logout') }}">注销 </a></li> {% else %} <li><a href="{{ url_for('login') }}" onclick="">登录</a></li> <li><a href="{{ url_for('regist') }}" onclick="">注册</a></li> {% endif %} <li><a href="{{ url_for('lable') }}" onclick="">发布问答</a></li> </ul> </div> <div style="float: right"> <img style="width: 50px; height: 50px" id="myonoff" onclick="mySwitch()" src="https://ps.ssl.qhimg.com/sdmt/119_135_100/t01ebad6510c93738fc.gif" style="width:50px" > </div> <div> <input type="text" name="user" id="user" placeholder="请输入搜索关键词"> <input type="button" value="搜索" class="btn btn-success" onclick=""> </div> {# <body background="http://p5.so.qhimgs1.com/bdr/_240_/t01fb87f1928a69d5eb.jpg" style="width: 100%; height: 100%"></body>#} </div> </nav> {% block main %}{% endblock %} </body > </html> </body>
user1.html
-
{% extends 'userbase.html' %} {% block user %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/userbase.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/user.css') }}"> {% endblock %} <div class="a1"> <h3>{{ username }}<br> <small>全部问答</small> </h3></div> <ul> {% for foo in user.question %} <li class="list-group-item" > <a href="#">{{ foo.author.username }} </a><br> <a href={{ url_for('detail',question_id=foo_id)}}>{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <p class="abstract">{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> {% endblock %}
user2.html
{% extends 'userbase.html' %} {% block user %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/userbase.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/user.css') }}"> {% endblock %} <div class="a1"> <h3>{{ username }}<br> <small>全部评论</small> </h3></div> <ul> {% for foo in user.comments %} <li class="list-group-item" > <a href="#">{{ foo.author.username }} </a><br> <a href={{ url_for('detail',question_id=foo_id)}}>{{ foo.title }}</a> <span class="badge">{{ foo.creat_time }}</span> <p class="abstract">{{ foo.detail }}</p> </li> {% endfor %} </ul> </div>
user3.html
{% extends 'userbase.html' %} {% block user %} {% block head %} <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/userbase.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static',filename='css/user.css') }}"> {% endblock %} <div class="a1"> <h3>{{ username}}<br> <small>个人信息</small> </h3></div> <ul> <li>用户:{{ username}}</li> <li>文章篇数:</li> </ul> {% endblock %}
- 个人中心—视图函数带标签页面参数tag
转载于:https://www.cnblogs.com/kebibrany/p/8074032.html