Home > Net >  Blog matching query does not exist
Blog matching query does not exist

Time:12-29

I am using slug to to show my blog details page.

here is models.py

class Blog(models.Model):
    author=models.ForeignKey(User,on_delete=models.CASCADE,related_name='post_author')
    blog_title=models.CharField(max_length=264,verbose_name='Put a Title')
    slug= models.SlugField(max_length=264,unique=True)
    blog_content=models.TextField(verbose_name='what is on your mind?')
    blog_image=models.ImageField(upload_to='blog_images',verbose_name='Image')
    publish_date=models.DateTimeField(auto_now_add=True)
    update_date=models.DateTimeField(auto_now=True)
    class Meta:
        ordering = ('-publish_date',)
    
    def __str__(self):
        return self.blog_title '  From :' str(self.author)

blog list views.py

def Creating_blog(request):
    form=BlogForm()
    if User.is_authenticated:
        if request.method=='POST':
                form=BlogForm(request.POST,request.FILES)
                blog_obj=form.save(commit=False)
                blog_obj.author=request.user
                title=blog_obj.blog_title
                blog_obj.slug = title.replace(' ','-')   '-'  str(uuid.uuid4())
                blog_obj.save()
                return redirect('bloglist')
    return render(request,'blogs/createblog.html',{'form':form})
     

blog details views.py

def blog_details(request, slug):
    if User.is_authenticated:
        blog= Blog.objects.get(slug=slug)
        already_liked=Likes.objects.filter(blog=blog,user=request.user)
        if already_liked:
            like=True
        else:
            like=False
        comments=Comment.objects.filter(blog=blog)
        commet_form=CommentForm()
        if request.method=='POST':
          commet_form=CommentForm(request.POST)
        if commet_form.is_valid():
            comment=commet_form.save(commit=False)
            comment.user=request.user
            comment.blog=blog
            comment.save()
            return HttpResponseRedirect(reverse('blog_details',kwargs={'slug':slug}))
        return render(request,'blogs/blogdetails.html',context={'blog':blog,'comment_form':commet_form,'comments':comments,'like':like})
    else:
        HttpResponseRedirect(reverse('login'))

urls.py

from django.urls import path
from .import views

urlpatterns = [
    path('write/',views.Creating_blog,name='creatingblogs'),
    path('bloglist/',views.BlogList.as_view(),name='bloglist'),
    path('details/<slug:slug>/',views.blog_details,name="blog_details")
]

blog_list page html anchor tag code

<div >
        <p>{{blog.blog_content|capfirst|truncatewords:100}}<a href="{% url 'blog_details' slug=blog.slug|slugify %}">Read More</a></p>
    </div>

I can see the blog list page but when it try to see blog details it shows me this error

DoesNotExist at /details/big-time-space-rarity-9a3d63b4-634a-11ec-9db9-9c7bef6a0e62/
Blog matching query does not exist.

I have tried adding some new blog but it shows me the same. please help me out this and kindly ask me if any description needed. Thanks in advance.

CodePudding user response:

The error occurs inside the blog_details view in the row:

blog= Blog.objects.get(slug=slug)

You should debug this part of the code and determine why you can't find the Blog instance by slug.

CodePudding user response:

It's quite a wild guess as I'm not reproducing your code but in my opinion something with your slug seems wrong.

First change the following line:

blog_obj.slug = slugify(title   '-'   str(uuid.uuid4()))

see: https://docs.djangoproject.com/en/4.0/ref/utils/#django.utils.text.slugify

then change the following:

<p>{{blog.blog_content|capfirst|truncatewords:100}}<a href="{% url 'blog_details' slug=blog.slug %}">Read More</a></p>

(remove slugify in the template)

Let me know if this worked. Also, if you need help with class based views, just let us know.

  • Related