2016-09-27 6 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が表示され、最初のテキストが表示されます。 2番目のタイトルには2番目のテキストが表示されます。 私はプログラムを書くべきですか、それはすべての投稿のための新しい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は投稿のURLを作成します。これは、ビューでdef post(argument)と定義されていますが、引数を指定していないものです。したがって、例えばIDに基づいてあなたの投稿を撮っているとすれば、あなたのビューはURLに/<int:post_id>/を要求し、post_idは特定の投稿を見つけるための引数として渡され、それをテンプレートに渡します。

url_forには、これが反映されている必要があります。{{ url_for('post', post_id=title[1]) }}またはpost_idに相当するものを保存している必要があります。

編集:あなたの編集にBaed

、あなたの問題は、あなたが取得するために投稿フラスコを語っていないことです。 IDまたはスラッグ、またはURLに入るものが必要で、あなたが探している投稿を教えてくれます。あなたの関数は静的で、データベース内の最初のエントリを常に取得しています。必要な変更は次のとおりです。

@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

関連する問題