activation_required render_to blog new_post html def create_post reque

 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
@activation_required()
@render_to('blog/new_post.html')
def create_post(request):
"""
show form for creation of new post and create post on submit
"""
if request.POST:
data = request.POST.copy()
data['author'] = request.user.id
form = CreatePostForm(data)
if form.is_valid():
post = form.save()
return HttpResponseRedirect(post.show_url())
else:
initial = {'author': request.user.id}
form = CreatePostForm(initial=initial)
return {'form': form}
class CreatePostForm(forms.ModelForm):
class Meta:
model = Post
exclude = ['date']
class Post(models.Model):
title = models.CharField(max_length=50)
content = models.TextField()
author = models.ForeignKey(Profile)
date = models.DateTimeField(auto_now_add=True)
sticky = models.SmallIntegerField(default=0)
comment_counter = models.IntegerField(default=0, blank=False, editable=False)
show_url = lambda self: reverse("post", args=[self.id])
class Meta:
ordering = ['-date']
def __unicode__(self):
return self.title