从首页问答标题到问答详情页

Posted 093陈泽琪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了从首页问答标题到问答详情页相关的知识,希望对你有一定的参考价值。

    1. 主PY文件写视图函数,带id参数。 
      @app.route(‘/detail/<question_id>‘)
      def detail(question_id):
          quest = 
          return render_template(‘detail.html‘, ques = quest) 
    2. 首页标题的标签做带参数的链接。
            {{ url_for(‘detail‘,question_id = foo.id) }}
      from flask import Flask, render_template,request,redirect,url_for,session
      from flask_sqlalchemy import SQLAlchemy
      import config
      from functools import wraps
      from datetime import datetime
      
      app = Flask(__name__)
      app.config.from_object(config)
      db = SQLAlchemy(app)
      
      
      class User(db.Model):
          __tablename__ = user
          id = db.Column(db.Integer, primary_key=True, autoincrement=True)
          username= db.Column(db.String(100), nullable=False)
          password= db.Column(db.String(500), nullable=False)
      
      
      class Question(db.Model):
          __tablename__ = question
          id = db.Column(db.Integer, primary_key=True, autoincrement=True)
          title= db.Column(db.String(100), nullable=False)
          detail = db.Column(db.Text, nullable=False)
          create_time = db.Column(db.DateTime, default=datetime.now )
          author_id = db.Column(db.Integer, db.ForeignKey(user.id))
          author=db.relationship(User,backref=db.backref(question))
      
      
      class Comment(db.Model):
          __tablename__ = comment
          id = db.Column(db.Integer, primary_key=True, autoincrement=True)
          author_id = db.Column(db.Integer, db.ForeignKey(user.id))
          question_id = db.Column(db.Integer, db.ForeignKey(question.id))
          create_time = db.Column(db.DateTime, default=datetime.now)
          detail = db.Column(db.Text, nullable=False)
          question = db.relationship(Question, backref=db.backref(comments))
          author = db.relationship(User, backref=db.backref(comments))
      
      db.create_all()
      
      
      @app.route(/)
      def hello_world():
          return render_template(base.html)
      @app.route(/zhuce/,methods=[GET,POST])
      
      def zhuce():
          if request.method == GET:
              return render_template(zc.html)
          else:
              username = request.form.get(username)  # 获取form中的数据
              password = request.form.get(password)  # 获取form中的数据
              # email = request.form.get(‘email‘)  # 获取form中的数据
              user = User.query.filter(User.username ==username).first()
              if user:
                  return用户已存在
              else:
                  user = User(username = username,password=password)
                  db.session.add(user)
                  db.session.commit()
                  return redirect(url_for(denglu))
      
      @app.route(/denglu/,methods=[GET,POST])
      def denglu():
          if request.method==GET:
              return render_template(dl.html)
          else:
              username = request.form.get(username)
              password = request.form.get(password)
      
              user=User.query.filter(User.username==username).first()
              #判断用户名是否存在
              if user:
                  session[user]=username
                  session.permanent=True
                  if user.password==password:
                      return redirect(url_for(shouye))
                  else:
                      return 密码错误
              else:
                  return 此用户不存在
      
      @app.context_processor
      def mycontext():
          usern=session.get(user)
          if usern:
              return {username:usern}
          else:
              return{}
      
      @app.route(/logout/)
      def logout():
          session.clear();
          return redirect(url_for(shouye))
      
      def log(func): #参数是函数
          @wraps(func)
          def wrappers(*args,** kwargs): #定义个函数将其返回
              if session.get(user):
                  return func(*args,**kwargs)
              else:
                  return redirect(url_for(denglu))
          return wrappers #返回一个函数
      
      
      @app.route(/shouye/)
      def shouye():
          context = {
              questions:Question.query.order_by(create_time).all()
          }
          return render_template(hh.html,**context)
      
      @app.route(/wenda/,methods=[GET,POST])
      @log
      def wenda():
          if request.method==GET:
              return render_template(wd.html)
          else:
              title=request.form.get(title)
              detail=request.form.get(detail)
              author_id=User.query.filter(User.username==session.get(user)).first().id
              question=Question(title=title,detail=detail,author_id=author_id)
              db.session.add(question)#保存到数据库
              db.session.commit()#提交
          return redirect(url_for(shouye))#跳转
      
      @app.route(/detail/<question_id>)
      def detail(question_id):
          quest=Question.query.filter(Question.id==question_id).first()
          return render_template(detail.html,ques=quest)
      
      if __name__ == __main__:
          app.run(debug=True)
    3. 在详情页将数据的显示在恰当的位置。 
      {{ ques.title}}
      {{ ques.id  }}{{  ques.creat_time }}
      {{ ques.author.username }} 
      {{ ques.detail }}
    4. 建立评论的对象关系映射:

      class Comment(db.Model):
          __tablename__=‘comment‘

    5.  尝试实现发布评论。

      {% extends ‘base.html‘ %}
      {% block title %}
          问答详情
      {% endblock %}
      {% block main %}
          <div class="box">
              <h3 href="#" >{{ ques.title }}</h3><small> {{ ques.author.username }}<span class="badge" style="margin-left: 75%">{{ ques.create_time }}</span></small>
              <hr>
              <p>{{ ques.detail }}</p>
              <hr>
              <form>
                  <div><textarea class="form-control" id="comment" rows="3" style="margin-left: 1%" name="comment" placeholder="write your comment"></textarea><br></div>
                  <button type="submit" >发送</button>
              </form>
            <h4>评论:</h4>
               <ul class="list-group">
                      <li class="list-group-item">
                          <img style="width: 30px" src="{{ url_for(‘static‘,filename=‘css/touxiang.jpg‘) }}" alt="64">
                          <a href="#"></a><br>
                          <p style="align-content: center"></p>
                          <span class="badge" style="margin-left: 60%"></span>
                          <p style="margin-left: 25%"></p><br>
                      </li>
              </ul>
      
          </div>
      {% endblock %}

       

以上是关于从首页问答标题到问答详情页的主要内容,如果未能解决你的问题,请参考以下文章

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页

从首页问答标题到问答详情页