Code Monkey home page Code Monkey logo

flask-tutorial's Issues

第五章,可能的小问题

第五章的275行左右,user由数据库读出来后仍然是一个类,可能需要在render_template里面使用name=user.name才能够正常显示

@app.route('/')
def index():
    user = User.query.first()  # 读取用户记录
    movies = Movie.query.all()  # 读取所有电影记录
    return render_template('index.html', user=user, movies=movies)

或者修改第三章的模板格式?

第八章
login_manager.login_view = 'login' #大小写问题?改写为Login

关于第一章Git使用

在Git bash中直接env\Scripts\activate会报错command not found,查资料后知道Git最开始是在Linux平台用的,移植到Windows上,盘符依然沿用Linux的斜杠(/),所以那个命令改成source venv/Scripts/activate 就可以正常激活虚拟环境了。

另外阅读短教程《Git 简明指南》现在似乎无法访问。

视图函数传参和使用flask.request获取参数的区别?

hello,
请问,在很多程序中的视图函数中都看到 使用 flask.request来获取参数的值,名称等等操作。但是,也有看到一些是对flask的视图函数进行传参来获取参数的。例如这样:
@app.route('/goback/<int:year>')
def go_back(year):
return 'Welcome to %d!' % (2018 - year)

这两种有什么区别吗?

第9章代码报错

第9章第二段代码中有个

from module_foo import sayhello

但是没有提到module_foo是什么?所以出现了错误提示 No module named 'module_foo'

第9章测试章节代码没有按预期执行

章节:第9章
位置:test_watchlist.py:测试认证相关功能

class WatchlistTestCase(unittest.TestCase):
    # ...
    # 测试登录保护
    def test_login_protect(self):
        response = self.client.get('/')
        data = response.get_data(as_text=True)
        self.assertNotIn('Logout', data)
......

这个测试用例不会通过,原因猜测可能是存在登录的缓存,我看了返回的html是登录过后的。于是我在前面加了个logout的方法,并且在这段代码前使用,即可通过测试:

    def logout(self):
        self.client.get('/logout')
......
    def test_login_protect(self):
        self.logout()
        response = self.client.get('/')
        data = response.get_data(as_text=True)
        self.assertNotIn('Logout', data)
        self.assertNotIn('Settings', data)
        self.assertNotIn('<form method="post">', data)
        self.assertNotIn('Delete', data)
        self.assertNotIn('Edit', data)

第三章的index.html代码有误,未及时更新

gitbook里的代码是

        <div data-gb-custom-block data-tag="for">  {# 迭代 movies 变量 #}
        <li>{{ movie.title }} - {{ movie.year }}</li>  {# 等同于 movie['title'] #}
        </div>  {# 使用 endfor 标签结束 for 语句 #}

跑出来抛了500错误,看了commit后发现代码并不一致

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>{{ name }}'s Watchlist</title>
</head>
<body>
    <h2>{{ name }}'s Watchlist</h2>
    <p>{{ movies|length }} Titles</p>
    <ul>
        {% for movie in movies %}
        <li>{{ movie.title }} - {{ movie.year }}</li>
        {% endfor %}
    </ul>
    <footer>
        <small>&copy; 2018 <a href="http://helloflask.com/tutorial">HelloFlask</a></small>
	</footer>
</body>
</html>

关于最后一章部署上线的一个小遗漏

第 11 章部署上线中, 其中这样写道:

最后安装依赖并执行初始化操作:

$ pipenv install  # 创建虚拟环境并安装依赖
$ pipenv shell  # 激活虚拟环境
$ flask initdb  # 初始化数据库
$ flask admin  # 创建管理员账户

这里在 flask initdb之后似乎少了flask forge这条命令用于生成 movies 数据.

另外, 非常感谢前辈的这一教程, 自己跟着做了一遍并加了一些别的小功能, 可以说受益匪浅学到了非常多, 这是我最后的成果: demo

真的非常感谢前辈的教程, 已经购买了前辈的书, 也打算继续跟进! 谢谢

第九章小错误

教程中测试初始化数据库的代码为:
image
应改为:
image

报错截图:
image

导入style.css无效

请问:我直接使用<link rel="stylesheet" href="{{ url_for('static',filename="style.css") }}" type="text/css">导入css文件,页面显示效果与教程不一致,但是我建了一个新的文件夹再把css文件放进去之后,使用<link rel="stylesheet" href="{{ url_for('static',filename="css/style.css") }}" type="text/css">引用css文件就好了,这是为什么?

错别字勘误

image
此处文字说明举例404, 实际代码举例说明400

小勘误:第 2 章:Hello, Flask!

从第1章一路代码敲过来到第2章,发现教程中在第2章的 flask run 之前,需要在终端中键入:

$ export FLASK_APP=app.py

不然会报错:

Usage: flask run [OPTIONS]

Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.

For more information see http://flask.pocoo.org/docs/latest/quickstart/
$ flask --version
Flask 0.12.2
Python 3.6.2 |Anaconda custom (x86_64)| (default, Sep 21 2017, 18:29:43)
[GCC 4.2.1 Compatible Clang 4.0.1 (tags/RELEASE_401/final)]

我更新到了最新版,似乎就解决了上述问题:

$ pipenv install flask==1.0.2

第10章第108页__init__.py 导入模块问题

按书上代码执行测试后,提示User not define
之后尝试在最后导入User和Movie模型类后错误解除
from watchlist.models import User,Movie

不知道是我其他代码输入有误,还是书这里导入遗漏了,还请作者指正。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.