Skip to content

Commit

Permalink
Ch17: Refactor Post Create and List into GCBV.
Browse files Browse the repository at this point in the history
  • Loading branch information
jambonrose committed Jul 30, 2015
1 parent 889de90 commit 18cf8cb
Showing 1 changed file with 6 additions and 27 deletions.
33 changes: 6 additions & 27 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,16 @@
get_object_or_404, redirect, render)
from django.views.decorators.http import \
require_http_methods
from django.views.generic import View
from django.views.generic import (
CreateView, ListView, View)

from .forms import PostForm
from .models import Post


class PostCreate(View):
class PostCreate(CreateView):
form_class = PostForm
template_name = 'blog/post_form.html'

def get(self, request):
return render(
request,
self.template_name,
{'form': self.form_class()})

def post(self, request):
bound_form = self.form_class(request.POST)
if bound_form.is_valid():
new_post = bound_form.save()
return redirect(new_post)
else:
return render(
request,
self.template_name,
{'form': bound_form})
model = Post


class PostDelete(View):
Expand Down Expand Up @@ -66,13 +50,8 @@ def post_detail(request, year, month, slug):
{'post': post})


class PostList(View):

def get(self, request):
return render(
request,
'blog/post_list.html',
{'post_list': Post.objects.all()})
class PostList(ListView):
model = Post


class PostUpdate(View):
Expand Down

0 comments on commit 18cf8cb

Please sign in to comment.