Skip to content
Advertisement

Django 1.6 No Category matches the given query

I am newbie in django .. and i dont understand what is reason this 404 error

I have Page not found (404) when i try go to link No Category matches the given query.

code:

models.py:

   ....
    def get_absolute_url(self):
        return ('article_url', None, { 'slug': self.slug })

   ....
      def get_absolute_url(self):
        return ('category_url', None, { 'slug': self.slug })

views.py:

def main(request):
    return render_to_response('main.html',
    {'categories': Category.objects.all(),
    'articles': Article.objects.all()[:4]})

def article_view(request, slug):
    return render_to_response('article_view.html',
    {'article': get_object_or_404(Article, slug=slug)})


def category_view(request, slug):
    category = get_object_or_404(Category, slug=slug)
    return render_to_response ('category_view.html',
    {'category': category, 'articles': Article.objects.filter(category=category)
    [:4]})

urls.py

urlpatterns = patterns('',
url(r'^$', 'main.views.main'),
url(r'^(?P<slug>[^.]+)/$', 'main.views.category_view', name='category_url'),
url(r'^(?P<slug>[^.]+)/$', 'main.views.article_view', name='article_url'),
)

Advertisement

Answer

Most likely, you don’t have category object with slug that you are specifying in the url. Due to this, the line

category = get_object_or_404(Category, slug=slug)

in your category_view() gives you 404 page.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement