Add profile name & about-me editing form

This commit is contained in:
Xevion
2022-03-27 09:37:05 -05:00
parent 5134723841
commit 0ce1560468
5 changed files with 48 additions and 3 deletions

View File

@@ -37,6 +37,9 @@ def create_app():
from .routes import blueprint as routes_blueprint from .routes import blueprint as routes_blueprint
app.register_blueprint(routes_blueprint) app.register_blueprint(routes_blueprint)
from .forms import blueprint as forms_blueprint
app.register_blueprint(forms_blueprint)
@app.errorhandler(404) @app.errorhandler(404)
def page_not_found(e): def page_not_found(e):
# note that we set the 404 status explicitly # note that we set the 404 status explicitly

24
forms.py Normal file
View File

@@ -0,0 +1,24 @@
from flask import Blueprint, flash, redirect, request, url_for
from flask_login import current_user, login_required
from create_app import db
from .models import User
blueprint = Blueprint('forms', __name__)
@blueprint.route('/user/<username>/edit', methods=['POST'])
@login_required
def edit_profile_post(username):
user = db.session.query(User).filter_by(username=username).first_or_404()
# Ignore non
if current_user.id != user.id:
return redirect(url_for('main.user', username=username))
user.about_me = request.form.get('about-me', user.about_me)
user.name = request.form.get('name', user.name)
db.session.commit()
flash('Successfully updated profile.')
return redirect(url_for('main.edit_user', username=username))

View File

@@ -7,7 +7,7 @@
</div> </div>
{% endif %} {% endif %}
{% endwith %} {% endwith %}
<form method="POST" action="/login" class="login-form"> <form method="POST" action="{{ url_for('auth.login_post') }}" class="login-form">
<div class="field"> <div class="field">
<div class="control"> <div class="control">
Username Username

View File

@@ -8,7 +8,7 @@
</div> </div>
{% endif %} {% endif %}
{% endwith %} {% endwith %}
<form method="POST" action="/signup" class="login-form"> <form method="POST" action="{{ url_for('auth.signup_post') }}" class="login-form">
<div class="field"> <div class="field">
<div class="control"> <div class="control">
Username Username

View File

@@ -1,5 +1,23 @@
{% extends 'layouts/index.html' %} {% extends 'layouts/index.html' %}
{% block content %} {% block content %}
<h3>Edit Profile</h3> <h3>Edit Profile</h3>
<form method="POST" action="{{ url_for('forms.edit_profile_post', username=user.username) }}" class="profile-form">
<label>
Name<br>
<input type="text" name="name" value="{{ user.name }}">
</label><br>
<label>
About Me
<textarea type="text" name="about-me">{{ user.about_me }}</textarea>
</label>
<button class="button">Save & Submit</button>
</form>
{% with messages = get_flashed_messages() %}
{% if messages %}
<div class="notification is-danger">
{{ messages[0] }}
</div>
{% endif %}
{% endwith %}
{% endblock %} {% endblock %}