Creating a blog with Flask
<p>I'm learning flask and I have a little problem.
I made an index template, where are blog post titles.</p>

<pre><code>{% for title in titles %}

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

                &lt;p class="post-meta"&gt;Posted by &lt;a href="#"&gt;{{ author }}&lt;/a&gt;&lt;/p&gt;
            &lt;/div&gt;
           &lt;/div&gt;
         &lt;/div&gt;
       &lt;/div&gt;

    {% endfor %}
</code></pre>

<p>Here is part of my post.html template.</p>

<pre><code>&lt;div class="container"&gt;
        &lt;div class="row"&gt;
            &lt;div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1"&gt;

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

<p>I'm using sqlite3. Currently every title leads to same post.html where is first text from first post. 
How to make every title direct to their post text? I mean, if I click on the first title it should bring up post.html and there should be first text. Second title should show second text. 
Should I write program, that creates new html for every post or is there any other way?</p>

<pre><code>@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])
</code></pre>

<p>The problem comes from here <code>&lt;a href="{{ url_for('post')}}"&gt;</code>.</p>

<p>What this tells Flask is to make a url for post, which is something you have defined in views as <code>def post(argument)</code> but you are not providing an argument. So if for example you are making you are taking your posts based on id, your view would would ask for <code>/&lt;int:post_id&gt;/</code> in url and <code>post_id</code> would be passed as an argument based on which you would find the specific post and pass that to the template. </p>

<p>Your <code>url_for</code> should reflect this, you should have <code>{{ url_for('post', post_id=title[1]) }}</code> or wherever you are storing your equivalent of post_id (maybe that's title for you).</p>

<p>Edit:</p>

<p>Baed on your edit, your problem is that you are not telling Flask which post to fetch. You need either ID, or slug, or something that will go in the url and will tell you which post you are looking for. Your function right now is static and is always fetching the first entry in your database. The changes required are:</p>

<pre><code>@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/&lt;int:post_id&gt;/')
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)


&lt;a href="{{ url_for('post', post_id=title[1])}}"&gt;
</code></pre>

<p>Also your author fetching is weird, you should have them stored (at least their ids) next to entries. Then I'd recomend some naming changes etc. It's hard to just answer the question and not write the code for you, as this is a site for answering specific questions, not writing code on demand :) Try to understand what I wrote here, play around with it a bit more etc. to fully undnerstand.</p>

<p>tl;dr: Posts have to get an argument and then fetch a post identified by that argument, the program can't magically tell which post you clicked on.</p>

python html flask