Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding "edit" functionality to the example app

  • Loading branch information...
commit 5c4fc5f639f368aef14174cfadfd160f8a5ae217 1 parent 92b4ec4
@coleifer authored
View
1  example/static/style.css
@@ -14,3 +14,4 @@ h2 { font-size: 1.2em; }
border: 1px solid #AACBE2; }
.avatar { display: block; float: left; margin: 0 10px 0 0; }
.message-content { min-height: 80px; }
+.message-edit { float: right; font-size: 0.6em; }
View
13 example/templates/edit.html
@@ -0,0 +1,13 @@
+{% extends "base.html" %}
+
+{% block content_title %}Edit message{% endblock %}
+
+{% block content %}
+ <form action="{{ url_for('edit', message_id=message.id) }}" method=post>
+ <dl>
+ <dt>Message:</dt>
+ <dd><textarea name="content">{{ message.content }}</textarea></dd>
+ <dd><input type="submit" value="Save" /></dd>
+ </dl>
+ </form>
+{% endblock %}
View
1  example/templates/includes/message.html
@@ -1,2 +1,3 @@
+{% if user and message.user == user %}<a class="message-edit" href="{{ url_for('edit', message_id=message.id) }}">edit</a>{% endif %}
<a class="avatar" href="{{ url_for('user_detail', username=message.user.username) }}"><img src="{{ message.user.gravatar_url() }}" /></a>
<p class="message-content">{{ message.content|urlize }}</p>
View
19 example/views.py
@@ -20,11 +20,11 @@ def homepage():
@auth.login_required
def private_timeline():
user = auth.get_logged_in_user()
-
+
messages = Message.select().where(
user__in=user.following()
).order_by(('pub_date', 'desc'))
-
+
return object_list('private_messages.html', messages, 'message_list')
@app.route('/public/')
@@ -46,7 +46,7 @@ def join():
)
user.set_password(request.form['password'])
user.save()
-
+
auth.login_user(user)
return redirect(url_for('homepage'))
@@ -110,3 +110,16 @@ def create():
return redirect(url_for('user_detail', username=user.username))
return render_template('create.html')
+
+@app.route('/edit/<int:message_id>/', methods=['GET', 'POST'])
+@auth.login_required
+def edit(message_id):
+ user = auth.get_logged_in_user()
+ message = get_object_or_404(Message, user=user, id=message_id)
+ if request.method == 'POST' and request.form['content']:
+ message.content = request.form['content']
+ message.save()
+ flash('Your changes were saved')
+ return redirect(url_for('user_detail', username=user.username))
+
+ return render_template('edit.html', message=message)
Please sign in to comment.
Something went wrong with that request. Please try again.