I am makeing a blog and the blog has a delete button. hen ever you click the button you are, spouse, to get taken back to the home page but you get this error name 'Blog_Post' is not defined. Ant help would be appreciated.
views.py
from django.shortcuts import render, redirect
from django.views import generic
from . import models
from django.contrib.auth import get_user_model
User = get_user_model()
from django.urls import reverse_lazy
from django.contrib.auth.mixins import LoginRequiredMixin
class create_blog_post(generic.CreateView):
model = models.Blog_Post
template_name = 'blog_app/creat_post.html'
fields = ('post_title', 'blog_content')
success_url = reverse_lazy('blog_app:all')
class view_blog_post(generic.DetailView):
model = models.Blog_Post
template_name = 'blog_app/view_post.html'
def delet_blog_post(request, id):
blog_post = Blog_Post.objects.get(id=blog_post_id)
blog_post.delete()
return redirect("/")
class all_blog_posts(generic.ListView):
model = models.Blog_Post
template_name = 'blog_app/all_posts.html'
#slug_url_kwarg = "slug"
CodePudding user response:
In this line, you don't declare blog_post_id anywhere Blog_Post.objects.get(id=blog_post_id)
You need blog_post_id = id. Or just id=id Where id is what you sent to the delete_blog_post function.
CodePudding user response:
Try this:
from django.contrib.auth import get_user_model
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views import generic
from .models import Blog_Post
User = get_user_model()
class create_blog_post(generic.CreateView):
model = Blog_Post
template_name = 'blog_app/creat_post.html'
fields = ('post_title', 'blog_content')
success_url = reverse_lazy('blog_app:all')
class view_blog_post(generic.DetailView):
model = Blog_Post
template_name = 'blog_app/view_post.html'
def delet_blog_post(request, id):
blog_post = Blog_Post.objects.get(id=blog_post_id)
blog_post.delete()
return redirect("/")
class all_blog_posts(generic.ListView):
model = Blog_Post
template_name = 'blog_app/all_posts.html'
#slug_url_kwarg = "slug"