TemplateSyntaxError in Python Flask

In summary, the error is that you can't put a positional argument after a keyword argument in a Python function call.
  • #1
rgtr
92
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
html code:
<h2> <a href="{{ url_for('userinfo.profile', username=current_user.username, 'update_profile') }}"> update_profile_info </a> </h2>
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



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)
update profile route

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()
 
Last edited:
Technology news on Phys.org
  • #2
We need to know the exact and entire error message and all the code that it references.
 
  • #3
I will upload my github and the entire error message at a later time. Thanks
 
  • #4
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
  • #5
rgtr said:
html code:
<h2> <a href="{{ url_for('userinfo.profile', username=current_user.username, 'update_profile') }}"> update_profile_info </a> </h2>

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'?
 
  • #6
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
  • #7
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.
 
  • #8
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.

 
  • #9
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.
 

FAQ: TemplateSyntaxError in Python Flask

What is a TemplateSyntaxError in Python Flask?

A TemplateSyntaxError in Python Flask is an error that occurs when there is an issue with the syntax of a template file in a Flask application. This error is raised when the template file contains invalid or incorrect syntax, making it unable to be rendered by the Flask application.

How do I fix a TemplateSyntaxError in Python Flask?

To fix a TemplateSyntaxError in Python Flask, you will need to carefully review the template file and identify the line or lines of code that are causing the error. Common causes of this error include missing or incorrect syntax for control structures, such as if/else statements or for loops. Once you have identified the issue, make the necessary corrections and save the file.

Can a TemplateSyntaxError be caused by other factors besides syntax errors?

Yes, a TemplateSyntaxError can also be caused by issues such as missing or incorrect template files, incorrect file paths, or incorrect template rendering methods. These issues can also result in the template file not being able to be rendered by the Flask application, leading to a TemplateSyntaxError.

How can I prevent TemplateSyntaxErrors in my Flask application?

To prevent TemplateSyntaxErrors in your Flask application, it is important to carefully review and test your template files before running the application. Additionally, using a code editor or IDE with syntax highlighting for Flask templates can help catch any syntax errors before they occur. Regularly testing and debugging your application can also help identify and fix any potential syntax errors.

Is a TemplateSyntaxError a common issue in Flask development?

Yes, a TemplateSyntaxError can be a common issue in Flask development, especially for beginners. This is because Flask templates use a different syntax than other web development frameworks, and it may take some time to become familiar with it. However, with careful attention to detail and regular testing, TemplateSyntaxErrors can be easily avoided in Flask applications.

Similar threads

Replies
2
Views
1K
Back
Top