Solution 1 :

So it seems you are headed in the right direction.

Try this:

  1. In your index.html, add a script at the header of the page/top of the page. You want the script to be loaded 1st
  2. So when a user goes to index.html the script checks whether local storage.getItem(“displayName”) exists.
  3. If it does, redirect to channels.html

Once you have tried that, and made the appropriate changes to your code, and if it did not work please edit your original post with the things you have now tried, as well as stack trace of any errors.
I am more than happy to take a look at it again and rework my answer.

Updates

So I believe the reason it is not working on your side currently has to do with the fact that in your channels function you need to have a return value for your second render template, like so :

@app.route("/channels", methods=['GET', 'POST'])
def channels():
    if request.method == "POST":
        name = request.form.get("display_name")
        return render_template("channels.html", name=name)
    return render_template("channels.html") # This line has been modified

Another change that you need to make in your code for the changes to take effect is to your index.js file. Just add this function.

//This function needs to be added to index.js
window.onload = function(){
if(localStorage.getItem("displayname")){

    window.location.replace("/channels");


}

I have implemented the above changes on my end and it seems to work as expected please let me if that works out for you. Also, on a different note, the best way to test this if it is working is using incognito mode.

Problem :

I am trying to create a flask web application where if the user visits my application for the first time, he will be directed to an index.html page, where he will be prompted to enter a name and submit it. Then I save the name in local storage and redirect the user to a second html page channels.html. Now if the user closes the tab and visits the application for a second time, he should be redirected to the second page channels.html, i.e., the "/chanels" route automatically instead of the first page.

Here is the app.py python file I have written so far:

@app.route("/")
def index():
    return render_template("index.html")

@app.route("/channels", methods=['GET', 'POST'])
def channels():
    if request.method == "POST":
        name = request.form.get("display_name")
        return render_template("channels.html", name=name)
    render_template("channels.html")

Here is my index.html:

<script src="{{url_for('static', filename='index.js')}}"></script>
<h2>Enter your name!</h2>
<form action="/channels" method='Post'>
            <input id="displayname" name="display_name" placeholder="Name" autocomplete="off" autofocus required type="text"/>
            <input type="submit" id="submitbutton" onclick="store()"/>
</form>

And index.js:

function store(){
    var name= document.getElementById("displayname");
    localStorage.setItem("displayname", name.value);
}

I tried googling how to do it, but couldn’t find a solution.
Any help would be kindly appreciated.

EDIT: On suggestions of @AkibRhast, I tried checking whether localStorage is there or not, and if yes, redirect to channels.html, so I added the following code to my index.js file:

if(localStorage.getItem("displayname")){
    window.location.replace("/channels");
}

But now whenever I visit the index page, I get a 404 Not Found error, although the url in the url bar has the link to channels, i.e. http://127.0.0.1:5000/channels.html.

Comments

Comment posted by tygzy

In the

Comment posted by Shrey Tripathi

@TylerH, I didn’t quite get your question. Yes, the

Comment posted by tygzy

I mean, in a website usually the

Comment posted by Shrey Tripathi

@TylerH, yes, that’s true…but do you think that’s gotta do something with my question?

Comment posted by tygzy

Not specifically your question but it can lead to issues further on if your routes aren’t

Comment posted by Shrey Tripathi

I tried doing this, but I now get a

Comment posted by Akib Rhast

@ShreyTripathi I have modifed my original answer please try implementing the changes suggested and let me know if it works

Comment posted by Akib Rhast

@ShreyTripathi On a different note, are you trying a redirect based on login status?

Comment posted by Shrey Tripathi

Didn’t quite get you. What do you mean by login status?

Comment posted by Shrey Tripathi

Also, thanks for your suggestion. That helped. One thing: why did you say that the best way to test this is using incognito mode? What difference does it make, in this case?

By

Leave a Reply

Your email address will not be published. Required fields are marked *