2016-09-27 54 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