Python TemplateSyntaxError in Python Flask

  • Thread starter Thread starter rgtr
  • Start date Start date
  • Tags Tags
    flask Python
AI Thread Summary
The discussion centers around a Python Flask error encountered in a Jinja2 template, specifically a TemplateSyntaxError related to the use of the `url_for` function. The problematic line attempts to create a link to the update profile route but incorrectly includes a positional argument after a keyword argument, which is not allowed in Python. The user is trying to link to the update profile route defined as `@userinfo.route("/profile/<string:username>/update_profile", methods=['POST', 'GET'])`. The error was identified as stemming from an incorrect query in the code, where the user initially used `posts = User.query.filter_by...` instead of `posts = Posts.query.filter_by...`. The discussion highlights the need for clarity in defining routes and constructing URLs using `url_for`, emphasizing that the route structure does not need to change as long as the correct parameters are passed. Further clarification on the exact error message and additional code references is requested to assist in troubleshooting.
rgtr
Messages
90
Reaction score
8
TL;DR Summary
jinja2.exceptions.TemplateSyntaxError: invalid syntax for function call expression The line above is some of the code in profile.html This happens when I click on the profile link.
This is in python flask. I am getting an error
line 16, in template [CODE lang="html" title="html code"]<h2> <a href="{{ url_for('userinfo.profile', username=current_user.username, 'update_profile') }}"> update_profile_info </a> </h2> [/CODE]
jinja2.exceptions.TemplateSyntaxError: invalid syntax for function call expression The line above is some of the code in profile.html This happens when I click on the profile link. How do I fix this?

routes.py

profile route



[CODE title="python flask"]@userinfo.route('/profile/<string:username>', methods = ['GET'])
def profile(username):
user = User.query.filter_by(username=username).first_or_404()
# profilepicture = request.files['profilepicture']

posts = Posts.query.filter_by(id=current_user.id).first()
return render_template('profile.html', title='profile', username=user.username, post_id=posts.post_id)[/CODE]
update profile route

[CODE lang="python" title="flask python"]@login_required

@userinfo.route("/profile/<string:username>/update_profile", methods = ['POST','GET'] )

def update_profile(username):

form = UpdateAccountForm if form.validate_on_submit():

# do I need this line? Yes to make sure the username exists.

username = User.query.filter_by(username=username).first_or_404()

password = form.password.data

confirm_password = form.confirm_password

# should I get the hashed passwords before checking them?

if password != confirm_password:

# need better phrasing

flash("Your passwords fields don't match.")

else:



hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())

# user = User.query.filter_by(hashed_password='hashed_password').first()

user = User(hashed_password=hashed_password)

db.session.add(user)

db.session.commit()[/CODE]
 
Last edited:
Technology news on Phys.org
We need to know the exact and entire error message and all the code that it references.
 
I will upload my github and the entire error message at a later time. Thanks
 
I found an error in the code. I had posts = User.query.filter_by... instead of posts = Posts.query.filter_by...

Here is the current error


Here is my github

https://github.com/NML240/flaskblog2/tree/masterIn app/userinfo/routes.py is the profile route.
The templates folder contain the html files.
 
Last edited:
  • Like
Likes sysprog
rgtr said:
[CODE lang="html" title="html code"]<h2> <a href="{{ url_for('userinfo.profile', username=current_user.username, 'update_profile') }}"> update_profile_info </a> </h2> [/CODE]

You can't put a positional argument after a keyword argument in a Python function call. What are you trying to do with , 'update_profile'?
 
sysprog said:
We need to know the exact and entire error message and all the code that it references.
Usually this is true, however in this case the error was trivial and easy to spot.
 
  • Like
Likes sysprog
pbuk said:
You can't put a positional argument after a keyword argument in a Python function call. What are you trying to do with , 'update_profile'?
I am trying to turn
@userinfo.route("/profile/<string:username>/update_profile", methods = ['POST','GET'] )
into a html link.
 
I assume @userinfo.route("/profile/<string:username>/update_profile", methods = ['POST','GET'] ) has to be @userinfo.route("/update_profile/<string:username>", methods = ['POST','GET'] ). I also noticed another error but I will fix that.

 
rgtr said:
I assume @userinfo.route("/profile/<string:username>/update_profile", methods = ['POST','GET'] ) has to be @userinfo.route("/update_profile/<string:username>", methods = ['POST','GET'] ). I also noticed another error but I will fix that.
Not really.
Python:
# If you have a route:
@app.route('/profile/<string:username>/update_profile', methods=['POST', 'GET'])
def it_does_not_matter_what_you_call_it(username):
    # Do stuff with `username`.
# then you can create a url to that route with:
url_for('it_does_not_matter_what_you_call_it', username=current_user.username)

# If you have a route:
@app.route('/update_profile/<string:username>', methods=['POST', 'GET'])
def it_does_not_matter_what_you_call_it(username):
    # Do stuff with `username`.
# then you can create a url to that route with:
url_for('it_does_not_matter_what_you_call_it', username=current_user.username)

See https://flask.palletsprojects.com/en/2.0.x/quickstart/#routing.
 
  • Like
Likes rgtr and sysprog
  • #10
Thanks.
 

Similar threads

Replies
2
Views
2K
Back
Top