Guys, tell me what's the problem? What are the solutions? I pass slug to urls and try to filter articles by category.

When you go to the category page gives an error:

DoesNotExist at /category/kino/ Posts matching query does not exist. Request Method: GET Request URL: http://127.0.0.1:8000/category/kino/ Django Version: 1.10.3 Exception Type: DoesNotExist Exception Value: Posts matching query does not exist. 

views.py

 def get_post(request, post_slug): post = Posts.objects.get(post_slug=post_slug) return render(request, 'page.html', {'post':post}) def get_category(request, category_slug): cat_id = get_object_or_404(Category, slug=category_slug) posts = Posts.objects.order_by('-pub_date').filter(id=cat_id, is_active=True) return render(request, 'category.html', {'posts':posts}) 

urls.py

 urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^$', index, name='home'), url(r'^(?P<post_slug>\S+)/$', get_post, name='post'), url(r'^category/(?P<category_slug>\S+)/$', get_category, name='category'), ] 

models.py

 class Posts(models.Model): class Meta: verbose_name = u'Статьи' verbose_name_plural = u'Статьи' title = models.CharField(max_length=160, verbose_name='Заголовок') body = models.TextField(verbose_name='Текст статьи') create_date = models.DateTimeField(auto_now_add=True, verbose_name=u'Дата создания', editable=False) pub_date = models.DateTimeField(auto_now=True, verbose_name=u'Дата публикации') is_active = models.BooleanField(default=False, verbose_name='Активация') post_slug = AutoSlugField(populate_from='title', unique=True) category = models.ForeignKey('Category', on_delete=models.PROTECT, default=False) def __str__(self): return '%s - %s'%(self.title, str(self.create_date)) class Category(models.Model): class Meta: verbose_name = u'Категория' verbose_name_plural = u'Категории' title = models.CharField(max_length=160, verbose_name='Название категории') parenth = models.ForeignKey('self', default=None, blank=True, null=True) slug = AutoSlugField(unique=True, populate_from='title') def __str__(self): return '%s (%s)'%(self.title, self.slug) 
  • .Fyvafyvafafafaffyf - Mr Fix

1 answer 1

DoesNotExist - exception, called when the requested record in the database was not found.

Try to fix it, it seems to me that the third instead of the fourth is being called.

 url(r'^(?P<post_slug>\[-\w]+)/$', get_post, name='post'), url(r'^category/(?P<category_slug>[-\w]+)/$', get_category, name='category'), 

\ w is a letter, number or underscore.

  • Such a problem that if I do S + in the url for a category, it works fine, because w + does not handle the "-" (hyphen) symbol. If the S + is in the URL of the article, then the article works, but the category does not. if I put w + in the article, then it does not open, it gives an url error. What could be the problem? - kelevra
  • In general, everything was decided with the remaining S + urls of both, but only the View and Url categories I moved up, put the first thing than the challenge of the article. And it all worked. I still do not understand what happened, why priorities are considered? I would like to understand why it worked, to realize. - kelevra
  • Yes, all right, the engine chooses the first thing that came up. - Mr. Fix
  • If you need to add a dash, then you need to take the allowed characters in square brackets. - Mr. Fix
  • Yes, like with a dash, it's quite just the rules of S +. And why did he consider it appropriate: url (r '^ (? P <post_slug> [- \ w] +) / $', get_post, name = 'post')? Here, the controller and post slug are completely different. - kelevra