W
W
wolverine7772021-04-30 19:53:11
Flask
wolverine777, 2021-04-30 19:53:11

How to register access to route from html for logged in user?

Hello, I apologize for the clumsy title, but I didn’t know how to write it differently yet.

It is necessary that each user after login can view the list of projects that he created.
So far, I've only been able to do this when I manually enter his name into a string, something like this: http://127.0.0.1:5000/name_of_user

I would like the user to be able to do this in a more convenient way - just by clicking on the " my projects " link.

So I have app.pyin

@app.route("/<first_name>")
@login_required    # to make sure that user must be logged in to see that view
def user_posts(first_name):

    #requesting a page
    page = request.args.get('page',1,type=int)
    user = User.query.filter_by(first_name=first_name).first_or_404()

    # filtering the blog posts by username
    projects = Project.query.filter_by(author=user).order_by(Project.date.desc()).paginate(page=page, per_page=5) # comes from backref='author' on models.py
    return render_template('user_projects.html', projects=projects, user = user)


inbase.html

{% if current_user.is_authenticated %}

          <li class="nav-link"> <a href="{{url_for('logout')}}">Log OUT</a> </li>

          <li class="nav-link"> <a href="{{url_for('project')}}">SUBMIT A PROJECT</a> </li>

          <li class="nav-link"> <a href="{{url_for('#')}}">my projects</a> </li>

        {% endif %}


What should be written in <a href="{{url_for('#')}}">my projects</a>?

This, of course, doesn't work.
<a href="{{url_for('<first_name>')}}">my projects</a>

This too
<li class="nav-link"> <a href="{{url_for('{{current_user.first_name}}')}}">my projects</a> </li>


I just would like the link to show the name of the logged in user.

Thanks

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-04-30
@GavriKos

Use sessions. You don't need to pass the username in the url. On the backend side, you need to understand who is knocking with the request. To do this, there are sessions / cookies, that's all. There was an excellent series of articles on Habré and it was discussed there

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question