0

I'm expanding upon miguel grinbergs tutorial and added custom profile picture logic. The image URL is saved as a column in the user model while using a .save() method to save the actual image before commiting.

This works, and I can see my new uploaded photo just fine. However when I return to my app a few hours later the image doesnt render.

Two things to note: I am having this problem on heroku only, not locally. Also, heroku is returning a 404 trying to access the image in /static/profile_pics/img.jpg which i do believe is where heroku looks for static.

// models.py -- user model

# User model
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
posts = db.relationship('Post', backref='author', lazy='dynamic')
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
followed = db.relationship(
    'User', secondary=followers,
    primaryjoin=(followers.c.follower_id == id),
    secondaryjoin=(followers.c.followed_id == id),
    backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
profile_pic = db.Column(db.String(128))

// routes.py - Edit profile route logic

@app.route('/edit_profile', methods=['GET', 'POST'])
@login_required
def edit_profile():

# Instantiate form
form = EditProfileForm(current_user.username)

# If submitting a POST request w/ valid data
# Updata data, commit to db.
if form.validate_on_submit():
    current_user.username = form.username.data
    current_user.about_me = form.about_me.data
    if form.profile_pic.data:
        pic = form.profile_pic.data
        filename = secure_filename(pic.filename)
        pic.save(os.path.join('app/static/profile_pics', filename))
        user = User.query.filter_by(username=current_user.username).update(dict(profile_pic=(
            os.path.join('/static/profile_pics/', filename))))
    db.session.commit()
    flash(_('Your changes have been saved.'))
    return redirect(url_for('edit_profile'))

# Pre-fill form data
elif request.method == 'GET':
    form.username.data = current_user.username
    form.about_me.data = current_user.about_me

return render_template('edit_profile.html', title=_('Edit Profile'),
                       form=form)

// RELEVANT USER.HTML

{% block app_content %}
<link rel="stylesheet" href="../static/css/explore.css">
<div class="grid-item item1">
    <table class="table table-hover">
        <tr>
            {% if user.profile_pic %}
            <td width="256px"><img style="border-radius: 50%;" width="256px" height="256px" src="{{ user.profile_pic }}"></td>
            {% else %}
            <td width="256px"><img style="border-radius: 50%" src="{{ user.avatar(256) }}"></td>
            {% endif %}
            <td>
                <h1>{{ _('User') }}: {{ user.username }}</h1>
                {% if user.about_me %}<p>{{ user.about_me }}</p>{% endif %}
                {% if user.last_seen %}
                <p>{{ _('Last seen on') }}: {{ moment(user.last_seen).format('LLL') }}</p>
                {% endif %}
                <p>{{ _('%(count)d followers', count=user.followers.count()) }}, {{ _('%(count)d following', count=user.followed.count()) }}</p>
                {% if user == current_user %}
                <p><a href="{{ url_for('edit_profile') }}">{{ _('Edit your profile') }}</a></p>
                <p><a href="{{ url_for('remove_user') }}">{{ _('Delete your account') }}</a></p>
                {% elif not current_user.is_following(user) %}
                <p><a href="{{ url_for('follow', username=user.username) }}">{{ _('Follow') }}</a></p>
                {% else %}
                <p><a href="{{ url_for('unfollow', username=user.username) }}">{{ _('Unfollow') }}</a></p>
                {% endif %}
user601206
  • 123
  • 7

0 Answers0