0

For the final project, I am trying to implement a flask application where people may write diaries and read others' diaries. I would like to create a unique URL for each user that shows the diaries that user published, however, I keep getting this HTTP 404 Not Found Error as I test-run the application. I think that is (not only) because every time I flask run the application and open up the web server, it is routed to https://ide50-chanchunhei.cs50.io/ instead of https://ide50-chanchunhei.cs50.io/index/username. If, instead of flask run the application and opening up the web server, I type http://ide50-chanchunhei.cs50.io/login into my browser, rather than getting back an HTTP 404 Not Found Error, I get this one: werkzeug.routing.BuildError: Could not build url for endpoint 'index'. Did you forget to specify values ['username']?. Anyways, my code is as follow:

application.py

@app.route("/index/<username>")
@login_required
def index(username):
    """Cover page: read your own diaries."""

    # lock the cover page until writer published first blog entry
    diaries = db.execute("SELECT diaries, date FROM diaries WHERE username = :username ORDER by date DESC", username=session.get("user_id"))
    if len(diaries) == 0:
     return dumbo("Welcome to FoToM!")

 return render_template("index.html", diaries=diaries, username=session.get("user_id"))

helpers.py

def login_required(f):
    """
    Decorate routes to require login.

    http://flask.pocoo.org/docs/0.11/patterns/viewdecorators/
    """

    @wraps(f)
    def decorated_function(*args, **kwargs):
        if session.get("user_id") is None:
            return redirect(url_for("login", next=request.url))
        return f(*args, **kwargs)
    return decorated_function

For your reference, my current code, unlike Problem Set 7, does not have any @app.route("/") now. And these are some online resources I made references to in implementing unique URLs: Link1, Link2, Link3, and Link4.

Thanks in advance!

0

Based on 1) what I don't know and 2) what you don't show:

  • Instead of https://ide50-chanchunhei.cs50.io/index/username, does https://ide50...../index?username=something work?

  • werkzeug.routing.BuildError: Could not build url for endpoint 'index'. Did you forget to specify values ['username'] indicates you are "calling" index something similar to pset7 code (return redirect(url_for("index"))), but are not sending the (required) username parameter. From URL Building in the Quickstart doc:

It accepts the name of the function as first argument and a number of keyword arguments,

As this example url_for('profile', username='John Doe') from the same source shows.

FWIW - this return render_template("index.html", diaries=diaries, username=session.get("user_id")) looks "loopy" (see 1) above). The function requires a username, so you can't automagically send it one after you've already called the function without a username.

ADDED:

  • 404 response is because there is no root path (@app.route("/"))
  • Time for the flask debugger? When it dumps the Traceback, look through the "blue" lines for the return redirect(url_for("index".... line. Hover and you will see a "console" icon at the right. Click that to open a console. (Find the pin in the flask terminal). One possibility is username=None because "If the value of a query argument is None, the whole pair is skipped.". But you'll need to use your eyes to examine, troubleshoot and debug. Good luck!
5
  • I specified not only the endpoint but also the variable argument every time I call a url_for for my index, but it still didn't work. At the most abstract level there are 2 problems, the first one being an HTTP 404 Not Found error whenever I flask run my app on the web server and the second one being the build error you mentioned when I manually type a URL, e.g. https://ide50-chanchunhei.cs50.io/login, to the browser. I should have given more details (my bad!). My current code is in dropbox.com/s/gq8b0i2db9zqzmd/…. THANKS!@Dino
    – Alex Chan
    Jul 26 '17 at 5:47
  • Added info to answer. Jul 26 '17 at 13:43
  • I took care of the 404 error (THANKS!). For the second issue, I followed your instructions, opened the console of the frame in which the build error was raised, and was able to confirm (by calling dump()) that username=None, but I looked through the online resources and just could not figure out why username=None. Would you give some more advice/tips? @Dino
    – Alex Chan
    Jul 29 '17 at 6:55
  • I don't know if it matters, but in addition to username=None, as are blueprint_name, _external, _anchor, and _method. @Dino
    – Alex Chan
    Jul 29 '17 at 7:04
  • Since username is set to session.get("user_id"), that means session.get("user_id") is None, that means there is no key user_id in session. You would surely have this error if you're coming from register. Jul 29 '17 at 12:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .