2016-09-27 96 views
0

我正在学习烧瓶,并且遇到了一些问题。 我做了一个索引模板,其中有博客文章标题。用Flask创建博客

{% for title in titles %} 

<!-- Main Content --> 
<div class="container"> 
    <div class="row"> 
     <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1"> 
      <div class="post-preview"> 
       <a href="{{ url_for('post')}}"> 
        <h2 class="post-title"> 
         {{ title[0] }} 
        </h2> 
       </a> 

       <p class="post-meta">Posted by <a href="#">{{ author }}</a></p> 
      </div> 
      </div> 
     </div> 
     </div> 

    {% endfor %} 

这是我的post.html模板的一部分。

<div class="container"> 
     <div class="row"> 
      <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1"> 

       <p>{{ post_text1 | safe }}</p> 
       <hr> 
       <div class="alert alert-info" role="alert">Posted by 
       <a href="#" class="alert-link">{{ author }}</a> 
       </div> 
      </div> 
     </div> 
    </div> 

我正在使用sqlite3。目前每个标题都会导致相同的post.html,其中第一个帖子是第一个文本。 如何使每个标题直接发布到他们的发布文本?我的意思是,如果我点击第一个标题,它应该显示post.html,并且应该有第一个文本。第二个标题应显示第二个文本。 我应该编写程序,为每个帖子创建新的html还是有其他方法?

@app.route('/') 
def index(): 
db = connect_db() 
titles = db.execute('select title from entries') 
titles = titles.fetchall() 
author = db.execute('select author from entries order by id desc') 
author = author.fetchone() 
return render_template('index.html', titles=titles[:], author=author[0]) 

@app.route('/post/') 
def post(): 
db = connect_db() 
post_text1 = db.execute('select post_text from entries') 
post_text1 = post_text1.fetchone() 
author = db.execute('select author from entries where id=2') 
author = author.fetchone() 
return render_template('post.html', post_text1=post_text1[0], author=author[0]) 
+0

你能发布你的索引和发布视图吗? –

回答

3

问题出在这里<a href="{{ url_for('post')}}">

这个告诉Flask是为post创建一个url,这是你在视图中定义的def post(argument),但是你没有提供参数。因此,例如,如果您要让自己的帖子基于id,则您的视图会在url中请求/<int:post_id>/,并且post_id将作为参数传递,根据该参数您可以找到特定帖子并将其传递给模板。

你的url_for应该反映这一点,你应该有{{ url_for('post', post_id=title[1]) }}或无论你在哪里存储相当于post_id(也许这是你的称号)。

编辑:

Baed您的编辑,你的问题是,你不告诉烧瓶中加入后去取。您需要ID或slug,或者将会在网址中显示的内容,并会告诉您您正在查找的帖子。您的功能现在是静态的,并且总是获取数据库中的第一个条目。所需要的变化是:

@app.route('/') 
def index(): 
db = connect_db() 
    titles = db.execute('select title, id from entries') 
    titles = titles.fetchall() 
    author = db.execute('select author from entries order by id desc') 
    author = author.fetchone() 
return render_template('index.html', titles=titles, author=author[0]) 

@app.route('/post/<int:post_id>/') 
def post(post_id): 
    db = connect_db() 
    post_text = db.execute('select post_text from entries where id = ?', post_id) 
    post_text = post_text1.fetchone() 
    author = db.execute('select author from entries where id=2') 
    author = author.fetchone() 
    return render_template('post.html', post_text1=post_text, author=author) 


<a href="{{ url_for('post', post_id=title[1])}}"> 

而且你的作者抓取是奇怪的,你应该将它们存储(至少它们的ID)旁边的条目。然后我会推荐一些命名更改等。很难只回答问题,也不会为您写代码,因为这是回答特定问题的网站,而不是按需编写代码:)尝试理解我在此处写的内容,多玩一些等等,完全没有意义。

tl; dr:帖子必须得到一个参数,然后获取由该参数标识的帖子,程序不能奇迹般地知道您点击了哪个帖子。

+0

我想我的python文件也有问题。我在上面发布了我的函数。 – Yakuzhy

+0

编辑的答案,希望有帮助! – iScrE4m

+0

非常感谢。我想出了一切! – Yakuzhy