TemplateSyntaxError in Python Flask

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

Discussion Overview

The discussion revolves around a TemplateSyntaxError encountered in a Python Flask application, specifically related to the use of Jinja2 templating in the profile route. Participants are exploring the error message and the relevant code snippets to identify the cause and potential solutions.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports a TemplateSyntaxError related to a function call in a Jinja2 template, specifically questioning the syntax used in the URL generation.
  • Another participant requests the complete error message and relevant code to better understand the issue.
  • A participant identifies a previous error in the code regarding the filtering of posts, suggesting it was incorrectly querying the User model instead of the Posts model.
  • Some participants point out that a positional argument cannot follow a keyword argument in Python function calls, questioning the intent behind the syntax used in the URL generation.
  • There are suggestions about modifying the route definition to potentially resolve the issue, but these suggestions are met with differing opinions on their validity.
  • A later reply provides examples of how to correctly use the `url_for` function with different route definitions, but does not reach a consensus on the best approach to take.

Areas of Agreement / Disagreement

Participants express differing views on the correct syntax and structure for the URL generation in the template, with no consensus reached on the best solution. Some agree on the syntax error, while others propose different interpretations of the routing structure.

Contextual Notes

Participants note that the discussion is based on specific code snippets and error messages, which may not encompass all potential issues or solutions. There are unresolved questions about the implications of changing route definitions and how they affect URL generation.

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