Answer the question
In order to leave comments, you need to log in
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.py
in
@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)
base.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 %}
<a href="{{url_for('#')}}">my projects</a>
? <a href="{{url_for('<first_name>')}}">my projects</a>
<li class="nav-link"> <a href="{{url_for('{{current_user.first_name}}')}}">my projects</a> </li>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question