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
redhatping
V2EX  ›  Python

web.py 下, get 和 post 同传入参数,导致错误,请问怎么解决?代码已贴

  •  
  •   redhatping · 2015-05-18 23:43:54 +08:00 · 2882 次点击
    这是一个创建于 3272 天前的主题,其中的信息可能已经有所发展或是发生改变。
    go.py

    ```python
    import web

    urls = ('/','Index')
    render = web.template.render('templates/')
    db= web.database( dbn='sqlite',db='movesite.db')


    class Index(object):
    def GET(self):
    return render.index(self)


    def POST(self):
    data=web.input()
    inputtext = r'name like"%' + data.title + r'%"'
    print inputtext
    move = db.select('move',where =inputtext)
    return render.index(move)

    if __name__ == '__main__':

    app = web.application(urls,globals())
    app.run()

    ```
    index.html
    如下:
    ```html
    $def with (movies)
    <html>
    <head>
    <meta charset="UTF-8">
    <title>hello world</title>
    </head>
    <body>

    <h1>豆瓣movies</h1>

    <form action = "/" method = "post">
    <input type ='text' name = 'title' />
    <input type='submit' value ='搜索'/>
    </form>

    $for move in movies:
    <li>$move.name
    </li>


    </body>
    </html>

    ```

    只要我去搜索了,系统就会错误,原因就是get,post冲突的, 可解决办法是post ,action去其他目录,但是我就要这个目录实现怎么办呢?

    2, 另外一个
    move = db.select('move',where =inputtext)

    type(move) ,发现是一个类实例, 但是类实例, 也可以迭代吗? 。
    8 条回复    2015-05-19 00:38:43 +08:00
    est
        1
    est  
       2015-05-18 23:59:11 +08:00
    redhatping
        2
    redhatping  
    OP
       2015-05-19 00:02:06 +08:00
    提问没什么丢人的, 可以百度的,当然迅速百度的, 还去微博@, 这不就是互联网,不如说我 爬书学习速度不错。。

    读书笔记这里:[python笔记](http://www.jianshu.com/notebooks/925220/latest)
    awanabe
        3
    awanabe  
       2015-05-19 00:12:33 +08:00
    @est
    楼主 在5天前的帖子里面回复... 会注意贴代码用markdown ...
    然后还是这样一坨代码...
    zhicheng
        4
    zhicheng  
       2015-05-19 00:19:31 +08:00
    1,完全不知道你在说什么。你确定你说的 GET 和 POST 冲突?这俩东西怎么个冲突法啊,我想知道。
    2,为什么类实例不能迭代,这是 1 + 1 = 2 的问题。
    lidashuang
        5
    lidashuang  
       2015-05-19 00:23:56 +08:00
    这种代码不格式化一下就是懒
    redhatping
        6
    redhatping  
    OP
       2015-05-19 00:32:57 +08:00
    @awanabe 我已经尝试了 ```python ```, 似乎这种markdown失效了, 我会检查一下下次
    redhatping
        7
    redhatping  
    OP
       2015-05-19 00:34:25 +08:00
    @zhicheng
    好的,

    贴主要代码一下:

    def get(self):
    return render.index ( 参数) ,没有参数不行,语法会报错。

    deg post(self):
    redhatping
        8
    redhatping  
    OP
       2015-05-19 00:38:43 +08:00
    def post(self):
    return.index(参数)

    在index,html中, 因为要读取参数中传递过来的值,$def with (name), 因为name 不知道 是对应get,post 参数的, 如果读取get的参数, 而index.html代码,对于参数进行迭代,就发现这个参数与哦问题。

    谢谢了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2260 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 07:54 · PVG 15:54 · LAX 00:54 · JFK 03:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.