【问题标题】:Creating a blog with Flask使用 Flask 创建博客
【发布时间】:2016-09-27 14:22:01
【问题描述】:

我正在学习烧瓶,我有一个小问题。 我做了一个索引模板,博客文章标题在哪里。

{% 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])

【问题讨论】:

  • 您能发布您的索引和视图吗?

标签: python html flask


【解决方案1】:

问题来自这里&lt;a href="{{ url_for('post')}}"&gt;

这告诉 Flask 是为帖子创建一个 url,这是您在视图中定义为 def post(argument) 的内容,但您没有提供参数。因此,例如,如果您要根据 id 发布帖子,您的视图会在 url 中要求 /&lt;int:post_id&gt;/ 并且 post_id 将作为参数传递,您将根据该参数找到特定的帖子并将其传递给模板。

您的 url_for 应该反映这一点,您应该有 {{ url_for('post', post_id=title[1]) }} 或您存储等效 post_id 的任何位置(也许这就是您的标题)。

编辑:

根据您的编辑,您的问题是您没有告诉 Flask 要获取哪个帖子。您需要 ID 或 slug 或将在 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:帖子必须获取一个参数,然后获取由该参数标识的帖子,程序无法神奇地判断您点击了哪个帖子。

【讨论】:

  • 我认为我的 python 文件也有问题。我在上面发布了我的功能。
  • 非常感谢。我想通了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 2014-07-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多