V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2018-11-24 13:06:59 +08:00 · 1077 次点击
    这是一个创建于 2225 天前的主题,其中的信息可能已经有所发展或是发生改变。

    [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 条回复    2018-11-26 09:42:49 +08:00
    zh826256645
        1
    zh826256645  
       2018-11-24 17:33:45 +08:00
    额,不知道怎么帮你好,多看看书吧
    masana
        2
    masana  
    OP
       2018-11-26 09:42:49 +08:00
    @zh826256645 感谢

    找到问题了,插入数据的冒号后面没有空格,/羞羞脸
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   968 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 22:52 · PVG 06:52 · LAX 14:52 · JFK 17:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.