# Class view Crud

### Create App blog

### models.py

```python
from django.db import models
from  django.contrib.auth.models import User


# Create your models here.

class Post(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(User,on_delete=models.CASCADE,null=True,blank=True)
```

### Add  Model To Admin Panel

**admin.py**

```python
from django.contrib import admin
from . models import Post
admin.site.register(Post)


```

### Urls.py

```python
from django.urls import path
from .  import views 

from . import views
urlpatterns=[
    path('',views.home,name='home'),
    path('posts/',views.PostList.as_view(),name='posts'),
    path('post/<int:pk>/',views.PostDetails.as_view(),name='post'),
    path('post/create/',views.CreateView.as_view(),name='post-create'),
    path('post/update/<int:pk>',views.PostUpdate.as_view(),name='post-update'),
    path('post/<int:pk>/delete',views.PostDelete.as_view(),name='post-delete')

]
```

### Views.py

```python
from django.shortcuts import render
from django.http import HttpResponse
from django.urls import reverse_lazy
from django.views.generic.list import ListView
from django.views.generic.detail import DetailView
from django.views.generic.edit import CreateView,UpdateView,DeleteView
from . models import Post
# Create your views here.
def home(request):
 return render(request,'blog/index.html')


class PostList(ListView):
    model = Post
    context_object_name = 'posts'
 


     

class PostDetails(DetailView):
    model = Post
    context_object_name = 'post'

class CreateView(CreateView):
    model=Post
    fields=['title','description']
    success_url = reverse_lazy('posts')


class PostUpdate(UpdateView):
    model = Post
    fields = ['title','description']
    success_url=reverse_lazy('posts')

class PostDelete(DeleteView):
    model = Post
    success_url = reverse_lazy('posts')
```

### Templates

#### templates/blog/**post\_form.html**

```python
{% extends 'base.html' %}
{% block content %}
<form method="post" novalidate>
    {% if post %} Update {% else %} Create {% endif %}
    {% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="submit">
</form>
{% endblock content %}
```

#### templates/blog/**post\_list.html** <a href="#blob-path" id="blob-path"></a>

```python
{% extends 'base.html' %}
{% block content %}
{% if posts %}
{% for post  in posts  %}
<h2> {{ post.title}}</h2>
<a href="{% url 'post' post.id %}">{{ post.title}}</a>
<a href="{% url 'post-update' post.id %}"> Edit </a>
<a href="{% url 'post-delete' post.id %}"> Delete </a>

<i class="bi bi-trash"></i>
{% endfor %}
{% endif %}
{% endblock content %}
```

#### templates/blog/**post\_detail.html** <a href="#blob-path" id="blob-path"></a>

```python
{% extends 'base.html' %}
{% block content %}
<article class="post">
    <header>
        {{ post.title }}
    </header>
    <p>{{ post.description }}</p>
</article>
{% endblock content %}
```

#### templates/blog/**post\_confirm\_delete.html** <a href="#blob-path" id="blob-path"></a>

```html
<form method="post">
    {% csrf_token %}
    <p>Are you sure you want to delete ""?</p>
        <input type="submit" value="Confirm">
    </form>
```
