0

I found this question( How to display a list of "latest pages visited" ) about how to display a list of latest pages visited for ruby on rails. How would you do this with flask? Do you use url maps? Code looks somehthing like this and i'm also using Heroku.

app = Flask(__name__)

app.secret_key = 'supersecretkey'

def login():
    session['username'] = "someuser"
    session['urls'] = []

@app.after_request
def store_visted_urls():
    session['urls'].append(request.url)
    if(len[session['urls']) > 5:
        session['urls'].pop(0)
    session.modified = True

@app.route('/')
def index():
    data = []
    if 'urls' in session:
        data = session['urls']
    return  render_template('page1.html',data=data)

@app.route('/page2', endpoint='page2')
def index():
    return  render_template('page2.html')

@app.route('/page3', endpoint='page3')
def index():
    return  render_template('page3.html')

if __name__ == '__main__':
    # Bind to PORT if defined, otherwise default to 5000.
    port = int(os.environ.get('PORT', 5000))
    app.run(host='0.0.0.0', port=port)

I want there to be a section on each of the pages (page1,page2,page3) that displays the last pages visited.

also, on this question ( Show recently visited html pages by any visitor ) someone suggested using cookies. is that a better way?

2 Answers 2

1

Try this:

url = request.url
if 'urls' in session:
    urls_list = session['urls']
    if not url in urls_list:
        urls_list.append(url)
        if len(urls_list) > 5:
            urls_list.pop(0)
            session['urls'] = urls_list
else:
     session['urls'] = [url]
1
  • Welcome to StackOverflow! Please edit your post to include an explanation for the code. This will make your answer more useful and more likely to be upvoted :)
    – Das_Geek
    Oct 9, 2019 at 13:57
0

You can use sessions if you want to store the user's page visit/URL history. After every request, store the visited URL in session. Something like:

secret key must be set to use sessions

app.secret_key = 'some secret key'

login function where you could initialize session variables.

def login():
    session['username'] = "someuser"
    session['urls'] = []

the function below uses the after_request decorator to store the visited urls after every user request.

@app.after_request
def store_visted_urls():
    session['urls'].append(request.url)
    if(len[session['urls']) > 5:
        session['urls'].pop(0)
    session.modified = True

View function to show last visted pages

@app.route('/')
def index():

data = []
if 'urls' in session:
data = session['urls']
return  render_template('page1.html',data=data)
6
  • I take it I have to 'from flask import after_request' at the beginning of the app? The flask documentation says "Register a function to be run after each request. Your function must take one parameter, a response_class object and return a new response object or the same" Do I have to pass some parameter to after_request? also, why are you using asterisks? My app is failing to load (page could not be served) when i implement your code. Nov 15, 2012 at 21:54
  • ignore the asterisks. I am having trouble with formatting the code sample when I paste here. after_request is just a python decorator. You can use it since you should have imported the flask module already. Also to serve the page, you need to use the route decorator. I did not provide an example for that earlier. See the update
    – codegeek
    Nov 15, 2012 at 22:03
  • i updated the code to show what mine looks like. It's still not happy with after_request decorator. Do I have to do something else to have the sessions reference the secret key? Is the problem that the flask app isn't allowed to run because the session[] doesn't have the key? Nov 15, 2012 at 22:48
  • unless you login, the session['urls'] wont be set. So in your index view, you should check for the key first.
    – codegeek
    Nov 15, 2012 at 22:51
  • also, can u set app.debug = TRUE to see the actual error stacktrace ?
    – codegeek
    Nov 15, 2012 at 22:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.