# Convert view to class-based

**URL:** <https://forum.djangoproject.com/t/convert-view-to-class-based/6457>\
**Category:** Using Django\
**Created:** [February 4, 2021, 6:57pm UTC](https://forum.djangoproject.com/t/convert-view-to-class-based/6457 "2021-02-04T18:57:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OmidShojaee](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/omidshojaee/32/2216_2.png) [@OmidShojaee](https://forum.djangoproject.com/u/OmidShojaee)\
**Post date:** [February 4, 2021, 6:57pm UTC](https://forum.djangoproject.com/t/convert-view-to-class-based/6457/1 "2021-02-04T18:57:40Z")

</div>

Hi,

I prefer to learn things by example.

Here’s my current view to retrieve a single post:

```auto
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?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 4, 2021, 7:25pm UTC](https://forum.djangoproject.com/t/convert-view-to-class-based/6457/2 "2021-02-04T19:25:52Z")

</div>

The official [Django Tutorial](https://docs.djangoproject.com/en/3.1/intro/tutorial01/) 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.

---

<div class="post-metadata">

**Author:** ![OmidShojaee](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/omidshojaee/32/2216_2.png) [@OmidShojaee](https://forum.djangoproject.com/u/OmidShojaee)\
**Post date:** [February 4, 2021, 7:29pm UTC](https://forum.djangoproject.com/t/convert-view-to-class-based/6457/3 "2021-02-04T19:29:38Z")

</div>

When I come here it always means I’ve read the official documentation and it didn’t help.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 4, 2021, 8:04pm UTC](https://forum.djangoproject.com/t/convert-view-to-class-based/6457/4 "2021-02-04T20:04:16Z")

</div>

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.
