笔记52 笨办法学python练习51继续建立与浏览器的关系,之三

**

笔记52笨办法学python练习51继续建立与浏览器的关系,笔记之三

**

一、重走一趟练习50的路程
1、在1pthw后建立flask目录
2、cd projects
3、mkdir gothonweb
4、cd gothonweb
5、mkdir bin gothonweb tests docs templates
6、touch gothonweb/init.py
7、touch tests/init.py
8、在templates下运行app.py
9、终端显示

在这里插入代码片PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb\templates> py app.py
 * Serving Flask app "app" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [08/Aug/2020 14:54:09] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Aug/2020 14:54:09] "[33mGET /favicon.ico HTTP/1.1[0m" 404 -
127.0.0.1 - - [08/Aug/2020 14:59:37] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Aug/2020 14:59:43] "[37mGET / HTTP/1.1[0m" 200 -
PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb\templates> py app.py
  File "app.py", line 6
    greeting =

10、浏览器显示greeting
11、去掉app.py中的第六行,重启
12、浏览器显示
Internal Server Error
13、终端显示
PS C:\Users\lenovo.venvs\1pthw\flask\projects\gothonweb\templates> py app.py

  • Serving Flask app “app” (lazy loading)
  • Environment: production
    WARNING: This is a development server. Do not use it in a production deployment.
    Use a production WSGI server instead.
  • Debug mode: off
  • Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
    [2020-08-08 15:04:23,004] ERROR in app: Exception on / [GET]
    Traceback (most recent call last):
    File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\flask\app.py”, line 2447, in wsgi_app
    response = self.full_dispatch_request()
    File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\flask\app.py”, line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\flask\app.py”, line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
    File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_compat.py”, line 39, in reraise
    raise value
    File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\flask\app.py”, line 1950, in full_dispatch_request
    rv = self.dispatch_request()
    File “C:\Users\lenovo\AppData\Local\Programs\Python\Python38\lib\site-packages\flask\app.py”, line 1936, in dispatch_request
    return self.view_functionsrule.endpoint
    File “app.py”, line 6, in hello_world
    greeting
    NameError: name ‘greeting’ is not defined
    127.0.0.1 - - [08/Aug/2020 15:04:23] “[35m[1mGET / HTTP/1.1[0m” 500 -

14、创建index.html文件和修改app、.py
浏览器上出现绿色greeting文字
{% if greeting %} I just wanted to say { { greeting }}. {% else %} Hello, world! {& endif %}

15、但每次执行app.py的结果都是internal server error
没有办法,前进到练习51

二、再读而且续练那个练习51
1、web工作原理,略去
2、将练习50的app.py改成form_test.py
3、在ps中执行form_test.py
目录templates下执行不动
4、在另一个目录tests下执行
依然执行不动
5、再换一个目录,退到哪一个都不行
6、按动ctrl C启动访问网页,启动不成功
几乎每次都出现拒绝访问,截图
笔记52 笨办法学python练习51继续建立与浏览器的关系,之三_第1张图片

7、建立表单hello_form.html
8、将练习50的app.py改为练习51的app.py
9、在目录templates下执行app
又是internal server error
在这里插入图片描述

10、将练习50中的index.html修改成index_laid_out.html
11、把hello_form.html修改成hello_form_laid_out.html
12、创建一个处于templates中的新文件layout.html
13、在tests中创建一个测试文件app_test.py
14、用nosetests通过
执行代码

在这里插入代码片PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb\templates> cd..
PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb> cd tests
PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb\tests> py app_tests.py
Traceback (most recent call last):
  File "app_tests.py", line 2, in 
    from app import app
ModuleNotFoundError: No module named 'app'
PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb\tests> nosetests

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

15、但在py中没有这个模块,搞不懂

在这里插入代码片PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb> cd tests
PS C:\Users\lenovo\.venvs\1pthw\flask\projects\gothonweb\tests> py app_tests.py
Traceback (most recent call last):
  File "app_tests.py", line 2, in 
    from app import app
ModuleNotFoundError: No module named 'app'

16、在templates目录下试试


在这里插入代码片PS C:\Users\lenovo.venvs\1pthw\flask\projects\gothonweb> cd templates
PS C:\Users\lenovo.venvs\1pthw\flask\projects\gothonweb\templates> py app_tests.py
C:\Users\lenovo\AppData\Local\Programs\Python\Python38\python.exe: can’t open file ‘app_tests.py’: [Errno 2] No such file or directory
PS C:\Users\lenovo.venvs\1pthw\flask\projects\gothonweb\templates>


17、我按照教材的要求安装了Apache服务器,但这个服务器的作用还体会不到。
可能失败了,但失败了也没有什么关系,继续前行到练习52.
我安装的Abache服务器
笔记52 笨办法学python练习51继续建立与浏览器的关系,之三_第2张图片
这个练习51折腾的时间够长的了,感概多多,暂且搁置吧。练习52开始。

你可能感兴趣的:(学习笔记,python)