I’ve been following the tutorial and for some reason at the point where the page is requesting the current_user’s image file, there is an ')
at the end of the image file name in the get request.
Here’s the model:
from flaskproject import db, login_manager
from datetime import datetime, timezone
from flask_login import UserMixin
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
image_file = db.Column(db.String(20), nullable=False, default="default.jpg")
password = db.Column(db.String(60), nullable=False)
posts = db.relationship("Post", backref="author", lazy="subquery")
def __repr__(self):
return f"User('id={self.id}', '{self.username}', '{self.email}', '{self.image_file}')"
routes.py
from flaskproject import app, db, bcrypt
from flaskproject.forms import RegistrationForm, LoginForm
from flask import render_template, redirect, flash, url_for, request
from flaskproject.models import Post, User
from flask_login import login_user, current_user, logout_user, login_required
@app.route("/my-account/")
@login_required
def my_account():
return render_template("my-account.html", title="My Account")
The html page:
<div>
<div>
<img src="{{ url_for('static', filename='profile-pics/' + current_user.image_file)}}')">
<div class="">
<h2 class="">{{ current_user.username }}</h2>
<p class="">{{ current_user.email }}</p>
</div>
</div>
<!-- FORM HERE -->
</div>
The file structure is:
foldername
--flaskproject
----static
------profile-pics
--------default.jpg
----templates
----__init__.py
----forms.py
----models.py
----routes.py
--run.py
__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt
from flask_login import LoginManager
app = Flask(__name__)
SECRET_KEY = "SECRET_KEY"
app.config["SECRET_KEY"] = SECRET_KEY
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///site.db"
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)
login_manager = LoginManager(app)
login_manager.login_view = "login"
login_manager.login_message_category = "danger"
from flaskproject import routes
run.py
from flaskproject import app
if __name__ == "__main__":
app.run(debug=True)
Then, when I load the my_account
route, the GET request gives me this
"GET /static/profile-pics/default.jpg') HTTP/1.1" 404 -
So, for some reason it includes ')
at the end as a part of the file name. I checked the database. Here’s what the image file looks like:
>>>user
User('id=1', 'admin', '[email protected]', 'default.jpg')
>>>user.image_file
'default.jpg'
It should be a normal string, right? At first the route was supposed to include that url_for('static')
and pass into the html page, but I changed it to what it is now, straight html url_for
, and nothing seemed different; the same ')
is at the end of the file name. What seemed most weird to me is that when I tried to do current_user.image_file[:-2]
, to cut the last 2 characters, and it cut 2 characters BEFORE the ')
.
What might be the reason for this ')
appearing at the end and ruining get request, and how do I fix it?
2