i have created a blog app. when i try to create new posts is shows the following error. i have double check everything. Now i really don’t know what to do.
here are project url.py= ```
path(‘admin/’, admin.site.urls),
path(‘register/’, user_views.register,name=‘register’),
path(‘profile/’, user_views.profile,name=‘profile’),
path(‘login/’, auth_views.LoginView.as_view(template_name=‘users/login.html’),name=‘login’),
path(‘logout/’,auth_views.LogoutView.as_view(template_name=‘users/logout.html’),name=‘logout’),
path(’’, include(‘Bblog.urls’)),
]
here are app url.py**
from django.urls import path
from . import views
from .views import PostListView,PostDetailView,PostCreateView,PostUpdateView,PostDeleteView,UserPostListView
urlpatterns = [
path('', PostListView.as_view(), name='blog-home'),
path('user/<str:username>/', UserPostListView.as_view(), name='user-posts'),
path('post/<int:pk>/', PostDetailView.as_view(), name='post-detail'),
path('post/new/', PostCreateView.as_view(), name='post-create'),
path('post/<int:pk>/update/', PostUpdateView.as_view(), name='post-update'),
path('post/<int:pk>/delete/', PostDeleteView.as_view(), name='post-delete'),
path('about/', views.about,name='blog-about'),
path('search/', views.search, name='blog-search'),
]
here are app view.py
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from .models import Post
from django.core.paginator import Paginator
from django.contrib.auth.models import User
from django.db.models import Q
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
def homepage(request):
context = {
'posts': Post.objects.all()
}
return render(request, 'Bblog/home.html', context)
class PostListView(ListView):
model = Post
template_name = 'Bblog/home.html'
context_object_name = 'posts'
ordering = ['-date_posted']
paginate_by = 3
class UserPostListView(ListView):
model = Post
template_name = 'Bblog/user_posts.html'
context_object_name = 'posts'
paginate_by = 3
def get_queryset(self):
user = get_object_or_404(User, username=self.kwargs.get('username'))
return Post.objects.filter(author=user).order_by('-date_posted')
class PostDetailView(DetailView):
model = Post
class PostCreateView(LoginRequiredMixin, CreateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Post
fields = ['title', 'content']
def form_valid(self, form):
form.instance.author = self.request.user
return super().form_valid(form)
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
class PostDeleteView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Post
success_url = '/'
def test_func(self):
post = self.get_object()
if self.request.user == post.author:
return True
return False
def about(request):
return render(request, 'Bblog/about.html', )
def search(request):
query = request.GET['query']
posts = Post.objects.filter(Q(title__icontains=query) | Q(content__icontains=query)
)
context = {
'posts': posts
}
return render(request, 'bblog/search.html', context)