__models py class Post models Model blog models ForeignKey Blog poster

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
__models.py
class Post(models.Model):
blog = models.ForeignKey(Blog)
poster = models.ForeignKey(User)
caption = models.CharField(max_length=50)
content = models.TextField()
posted = models.DateTimeField()
tags = models.ManyToManyField(Tag)
def __unicode__(self):
return self.caption
def get_absolute_url(self):
return "/archive/%s/%s" % (self.posted.strftime('%Y/%m/%d'), self.caption)
class Comment(models.Model):
post = models.ForeignKey(Post)
user = models.CharField(max_length=20)
content = models.TextField()
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return self.content
__forms.py
class CommentsForm(ModelForm):
class Meta:
model = Comment
exclude = ('post')
__views.py
def archives(request, year=None, month=None, day=None, caption=None):
entry = Post.objects.filter(posted__year=year, posted__month=month, posted__day=day, caption=caption)[0]
comments = Comment.objects.filter(post=entry.id)
if request.method == 'POST':
form = CommentsForm(request.POST)
if form.is_valid():
form.save()
else:
form = CommentsForm()
return render_to_response("entry.html", {'entry': entry, 'form': form})