0

I have the classes in main module

__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()

In login.POST , the form data is compared and if successful i need to redirect to feature.html which has

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

Using JINJA2 templating with web.py how can i redirect to feature.html with parameter 'user'. The above code works but 'user' is send as URL parameter .Basically i want to try web.py redirect with JINJA2 templating.Please help.

1 Answer 1

1

You need to pass your user information into the render.feature() call.

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

This works because web.input() will get the results of POST (when called within class login, and gets the URL parameters with a GET (when called within class feature.) So your redirect to /feature is working fine, but you need to pass the information into the template renderer so you can print the results!

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any other way to pass parameter while redirecting other than appending to URL ?
Have login.POST "create a session" by storing session_id and logged in user information into a database. Drop the session_id as a cookie. Then, in the feature.GET, read the cookie, do a database read converting session_id into your logged-in user's information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.