Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

permalink example #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added microblog.db
Binary file not shown.
18 changes: 16 additions & 2 deletions microblog.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,14 @@ def write_entry(title, text):

def get_all_entries():
con = get_database_connection()
cur = con.execute('SELECT title, text FROM entries ORDER BY id DESC')
return [dict(title=row[0], text=row[1]) for row in cur.fetchall()]
cur = con.execute('SELECT id, title, text FROM entries ORDER BY id DESC')
return [dict(id=row[0], title=row[1], text=row[2]) for row in cur.fetchall()]


def get_one_entry(entry_id):
con = get_database_connection()
cur = con.execute('SELECT title, text FROM entries where id = ?', entry_id )
return cur.fetchone()


def do_login(usr, pwd):
Expand Down Expand Up @@ -103,5 +109,13 @@ def add_entry():
return redirect(url_for('show_entries'))


@app.route('/permalink/<entry_id>')
def permalink(entry_id):
entry = get_one_entry(entry_id)
if entry:
return render_template('permalink.html', title=entry[0], text=entry[1])
else:
abort(404)

if __name__ == '__main__':
app.run(debug=True)
10 changes: 10 additions & 0 deletions templates/permalink.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "layout.html" %}
{% block body %}
{% if session.logged_in %}
<h2>{{ title }}</h2>
<p> {{ text }}</p>
{% else %}
<p>You must be logged in to see a permalink (although I'm not sure why).</p>
{% endif %}
<a href="{{ url_for('show_entries') }}">Home</a>
{% endblock %}
6 changes: 3 additions & 3 deletions templates/show_entries.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</div>
<div class="field">
<label for="text">Text</label>
<textarea name="text" id="text" rows="5" cols="80"></textarea>
<textarea name="text" id="text" rows="5" cols="70"></textarea>
</div>
<div class="control_row">
<input type="submit" value="Share" name="Share"/>
Expand All @@ -19,7 +19,7 @@ <h2>Posts</h2>
<ul class="entries">
{% for entry in entries %}
<li>
<h2>{{ entry.title }}</h2>
<h2><a href="{{ url_for('permalink', entry_id=entry.id) }}">{{ entry.title }}</a></h2>
<div class="entry_body">
{{ entry.text|safe }}
</div>
Expand All @@ -28,4 +28,4 @@ <h2>{{ entry.title }}</h2>
<li><em>No entries here so far</em></li>
{% endfor %}
</ul>
{% endblock %}
{% endblock %}