TemplateSyntaxError in Python Flask

  • Context: Python 
  • Thread starter Thread starter rgtr
  • Start date Start date
  • Tags Tags
    flask Python
Click For Summary
SUMMARY

The forum discussion addresses a TemplateSyntaxError encountered in a Python Flask application, specifically related to the improper use of positional and keyword arguments in the url_for function. The error arises from the line in profile.html: <a href="{{ url_for('userinfo.profile', username=current_user.username, 'update_profile') }}">, where the positional argument 'update_profile' is incorrectly placed after the keyword argument username. The correct approach is to ensure that all positional arguments precede keyword arguments in function calls.

PREREQUISITES
  • Understanding of Python Flask routing
  • Familiarity with Jinja2 templating syntax
  • Knowledge of function argument conventions in Python
  • Experience with Flask's url_for function
NEXT STEPS
  • Review Flask documentation on url_for usage
  • Learn about Jinja2 templating best practices
  • Explore error handling in Flask applications
  • Investigate common routing patterns in Flask
USEFUL FOR

Web developers, particularly those working with Python Flask, who are troubleshooting template rendering issues and improving their understanding of routing and function calls in web applications.

rgtr
Messages
90
Reaction score
8
TL;DR
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   Reactions: 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   Reactions: 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   Reactions: rgtr and sysprog
  • #10
Thanks.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K