Skip to content

Commit

Permalink
individual post view
Browse files Browse the repository at this point in the history
(As you can see, I think I'm going to resist the temptation to pass
locals() as a template context; yes, it's cool, but it is written that
Explicit Is Better Than Implicit.)
  • Loading branch information
zackmdavis committed Aug 24, 2014
1 parent 5762d6f commit 20c227f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 5 deletions.
2 changes: 2 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class Post(models.Model):
# TODO: "title" attribute should be CharField (but what should
# max_length be??)
content = models.TextField()
# TODO: date published (a DateTimeField), definitely! Maybe date
# last edited (DateTimeField with auto_now), too?

def __str__(self):
# TODO: change this to something more informative when we have
Expand Down
9 changes: 8 additions & 1 deletion core/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,11 @@

def home(request):
posts = Post.objects.all()
return render(request, "home.html", locals())
return render(request, "home.html", {'posts': posts})

def show_post(request, pk):
# TODO: looking up posts by ID number is super ugly; we probably
# want to store URL slugs in the post model (and index that
# column!) and look them up that way?
post = Post.objects.get(pk=pk)
return render(request, "post.html", {'post': post})
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<title>Finetooth &mdash; {% block subtitle %}{% endblock %}</title>
</head>
<body>
<h1>Finetooth</h1>
<h1><a href="/">Finetooth</a></h1>
{% block content %}{% endblock %}
</body>

Expand Down
4 changes: 2 additions & 2 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

{% block content %}
{% for post in posts %}
<h3>Post #{{ post.pk }}</h3>
<h3><a href="{{ post.pk }}/">Post #{{ post.pk }}</a></h3>
<div class="post" data-id="{{ post.pk }}">
{{ post.content }}
</div>
<a href="#TODO">
<a href="{{ post.pk }}/#comments">
<strong>{{ post.comment_set.count }} comments</strong>
</a>
{% endfor %}
Expand Down
22 changes: 22 additions & 0 deletions templates/post.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{% extends "base.html" %}

{% block subtitle %}{{ post.title }}{% endblock %}

{% block content %}
<h2>Post #{{ post.pk }}</h2>

<div class="post-content" data-id="{{ post.pk }}">
{{ post.content }}
</div>

<div id="comments">
<h4>{{ post.comment_set.count }} comments</h4>
{% for comment in post.comment_set.all %}
<hr>
<div class="comment" data-id="{{ comment.pk }}">
{{ comment.content }}
</div>
{% endfor %}
</div>

{% endblock %}
4 changes: 3 additions & 1 deletion urls.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.conf.urls import patterns, include, url

urlpatterns = patterns('',
urlpatterns = patterns(
'',
url(r'^$', 'core.views.home', name='home'),
url(r'^(\d+)', 'core.views.show_post', name="show_post")
)

0 comments on commit 20c227f

Please sign in to comment.