Hi,
I prefer to learn things by example.
Here’s my current view to retrieve a single post:
def post_detail(request, year, month, day, post):
post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month,
publish__day=day)
return render(request, 'blog/post/detail.html', {'post': post})
How to convert it to class-based?
The official Django Tutorial covers this topic, including examples.
If you have not yet worked your way through it, I recommend it highly. It presents a lot of useful information and important concepts.
When I come here it always means I’ve read the official documentation and it didn’t help.
The Tutorial isn’t something to just be read. To get the most benefit out of it, you need to actually work through it - and that includes typing all the examples instead of just copy/pasting the code snippets.
If you have already worked through the Tutorial, then you have a pattern for conversion that you can follow. If so, then I would suggest you try to create your own CBV for your view from that pattern. If it doesn’t work, then post your attempt here, and we can try to correct any mistakes you may have made along the way.
3 Likes