Home > OS >  posts.models.Blog.DoesNotExist: Blog matching query does not exist
posts.models.Blog.DoesNotExist: Blog matching query does not exist

Time:05-30

i am trying to use slug to navigate. here's the html tag

<a href="{% url 'blog_details' blog.slug|slugify %}">Read More</a>

urls.py

path('details/<slug:slug>/', views.blog_details, name='blog_details'),

models.py

slug= models.SlugField(max_length=264,unique=True,null=True,allow_unicode=True)

this is how i am creating auto slug

def Creating_blog(request):
    form=BlogForm()
    if User.is_authenticated:
        if request.method=='POST':
                form=BlogForm(request.POST,request.FILES)
                if form.is_valid:
                    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('index')
    return render(request,'blogs/creatingblog.html',context={'form':form})

this is the view of the blog details page

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:
           liked = True
        else:
          liked = 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':liked})
    else:
        HttpResponseRedirect(reverse('login'))

the problem is must be in

C:\Users\ITS\Desktop\blogger\Blog\posts\views.py, line 51, in blog_details
        blog = Blog.objects.get(slug=slug) 

if i use pk=id instead of slug, it works. But whenever i am trying to catch with the slug the error rises. i am stuck for days here i can't find what's wrong.

CodePudding user response:

By passing the slug through the |sligify template filter [Django-doc] it might transform some slugs, such that linking to the original slug is no longer possible. You should remove the template filter, and work with:

<a href="{% url 'blog_details' blog.slug %}">Read More</a>

But regardless, slugifying yourself is not a good idea. You can use Django's slugify(…) function [Django-doc] to do this properly. The view also contains some problems, for example User.is_authenticated will always have truthiness True, and thus never fail, and .is_valid() [Django-doc] is a method, so you need to call this:

from django.contrib.auth.decorators import login_required
from django.utils.text import slugify

@login_required
def creating_blog(request):
    form = BlogForm()
    if request.method == 'POST':
        form = BlogForm(request.POST, request.FILES)
        if form.is_valid():
            form.instance.author = request.user
            form.instance.slug = slugify(f'{form.instance.title} {uuid.uuid4()}')
            form.save()
            return redirect('index')
    return render(request, 'blogs/creatingblog.html', {'form':form})

Note: You can limit views to a view to authenticated users with the @login_required decorator [Django-doc].


Note: You can make use of django-autoslug [GitHub] to automatically create a slug based on other field(s).


Note: It is often better to use get_object_or_404(…) [Django-doc], then to use .get(…) [Django-doc] directly. In case the object does not exists, for example because the user altered the URL themselves, the get_object_or_404(…) will result in returning a HTTP 404 Not Found response, whereas using .get(…) will result in a HTTP 500 Server Error.

CodePudding user response:

Finally it works. I have just made a little changes. The anchor tags

<a href="{% url 'blog_details' slug=blog.slug %}">Read More</a>

after changing this one other error was

NoReverseMatch at /blogs/
Reverse for 'blog_details' with keyword arguments '{'slug': '9Analysing-the-FIA-Rulebook-after-the-Abu-Dhabi-Grand-Prix-—-A-Lawyer’s-Perspective-9861e7b8-1c14-4fdd-abb6-e2d8398cf318'}' not found. 1 pattern(s) tried: ['details/(?P<slug>[-a-zA-Z0-9_] )/$']

i changed the urls.py

path('details/(?P<slug>[-a-zA-Z0-9_] )/$', views.blog_details, name='blog_details'),

And then i works now. though in the terminal one warning rises

WARNINGS:
?: (2_0.W001) Your URL pattern 'details/(?P<slug>[-a-zA-Z0-9_] )/$' [name='blog_details'] has a route that contains '(?P<', begins with a '^', or ends with a 
'$'. This was likely an oversight when migrating to django.urls.path().

what it means by saying oversight to django.urls.path()?

  • Related