推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
masana
V2EX  ›  Python

新手请教,抄 BeProud 的教程实例的,运行 load_data 却没有数据也没报错,什么情况 在 Ubuntu 上的 python2.7.3 和 windows 的 python3.6.32 环境中都不行,代码如下:

  •  
  •   masana · Nov 24, 2018 · 1524 views
    This topic created in 2753 days ago, the information mentioned may be changed or developed.

    [code] #coding:utf-8 import shelve

    from flask import Flask, request, render_template, redirect, escape, Markup from datetime import datetime

    application = Flask(name)

    DATA_FILE = 'guestbook.dat'

    def save_data(name,comment,create_at): """ Save the comment data """ #open the shelve module database File

    database = shelve.open(DATA_FILE)
    #if there is no greeting list in database, create it.
    if 'greeting_list' not in database:
    	greeting_list = []
    else:
    	#get the greeting_list from database
    	greeting_list = database['greeting_list']
    
    	#append the data into the list top
    	greeting_list.insert(0, {
    	'name':name,
    	'comment':comment,
    	'create_at':create_at,
    	})
    
    	# update the database
    	database['greeting_list'] = greeting_list
    
    	#close the database file
    	database.close()
    

    def load_data(): """ Return the comment data saved before """ #open the shelve module database file database = shelve.open(DATA_FILE)

    #get the greeting_list. if not ,just return empty list.
    greeting_list = database.get('greeting_list',[])
    
    database.close()
    return greeting_list
    

    @application.route('/') def index(): """ Top page Use template to show the page """ greeting_list=load_data() return render_template('index.html',greeting_list=greeting_list)

    @application.route('/post',methods=['POST']) def post(): """ Comment's target url """ # get the comment data name = request.form.get('name') comment = request.form.get('comments') create_at = datetime.now() save_data(name, comment, create_at) #rediret to the top page return redirect('/')

    if name== 'main': #Run the application when the IP address is 0.0.0.0 and the port is 8000

    application.run('0.0.0.0',8000,debug=True)
    

    [/code]

    2 replies    2018-11-26 09:42:49 +08:00
    zh826256645
        1
    zh826256645  
       Nov 24, 2018
    额,不知道怎么帮你好,多看看书吧
    masana
        2
    masana  
    OP
       Nov 26, 2018
    @zh826256645 感谢

    找到问题了,插入数据的冒号后面没有空格,/羞羞脸
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3219 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 00:29 · PVG 08:29 · LAX 17:29 · JFK 20:29
    ♥ Do have faith in what you're doing.