Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Jinja2的Python web.py重定向_Python_Python 2.7_Jinja2_Web.py - Fatal编程技术网

使用Jinja2的Python web.py重定向

使用Jinja2的Python web.py重定向,python,python-2.7,jinja2,web.py,Python,Python 2.7,Jinja2,Web.py,我有主模块中的类 \uuuu init\uuuuu.py import web from web.contrib.template import render_jinja urls = ( '/', 'main.views.login', '/login', 'main.views.login', '/feature', 'main.views.feature' ) app = web.application(urls, globals()) render = render_jin

我有主模块中的类

\uuuu init\uuuuu.py

import web
from web.contrib.template import render_jinja

urls = (
  '/', 'main.views.login',
  '/login', 'main.views.login',
  '/feature', 'main.views.feature'
)
app = web.application(urls, globals())
render = render_jinja(
        'main/templates',
        encoding='utf-8',
    )
视图.py

from main import web, render
class login:
    def GET(self):
        return render.login(title="Login")

    def POST(self):
        data = web.input();
        userName = data['username']
        password = data['password']
        if((userName == 'viv') and (password == 'viv')):
            raise web.seeother('/feature?user=' + userName)
        return render.login(error="Login Failed !!!")
class feature:
    def GET(self):
        print(web.input())
        return render.feature()
在login.POST中,比较表单数据,如果成功,我需要重定向到feature.html

<div>Hello {{ user }}</div> 
Hello{{user}

使用带有web.py的JINJA2模板,如何重定向到带有参数“user”的feature.html。上面的代码可以工作,但“user”是作为URL参数发送的。基本上,我想尝试使用JINJA2模板进行web.py重定向。请提供帮助。

您需要将
用户
信息传递到
render.feature()
调用中

class feature:
    def GET(self):
        return render.feature(user=web.input().user)

这是因为
web.input()
将获得POST的结果(在
类登录中调用时
),并使用get获取URL参数(在
类功能中调用时
)因此,重定向到
/feature
的工作正常,但您需要将信息传递到模板渲染器中,以便打印结果!

在重定向时,除了附加到URL之外,还有其他方法传递参数吗?让login.POST“创建会话”通过将会话id和登录用户信息存储到数据库中。将会话id作为cookie删除。然后,在feature.GET中,读取cookie,执行数据库读取,将会话id转换为登录用户信息。