繁体   English   中英

使用Jinja2进行Python web.py重定向

[英]Python web.py redirect using Jinja2

我在主模块中有课程

__init__.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',
    )

views.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中,将比较表单数据,如果成功,则需要重定向到具有

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

如何将JINJA2模板与web.py一起使用,如何使用参数“ user”重定向到feature.html。 上面的代码有效,但是'user'是作为URL参数发送的。基本上我想尝试使用JINJA2模板进行web.py重定向。请帮忙。

您需要将user信息传递给render.feature()调用。

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

之所以web.input()是因为web.input()将获取POST的结果(在class login调用,并使用GET获取URL参数(当在class feature调用)。)因此,重定向到/feature可以正常工作,但是您需要将信息传递到模板渲染器中,以便您可以打印结果!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM