Category matching query does not exist. I don'

2019-09-09 12:06发布

I know the problem but I don't know why it does and how to fix this. if you know, please help me. here's what I'm doing. users can click update/popular. if user click update, posts are displayed in the order of time that got posted. if popular, then it's displayed in the score they receive. I was managed to achieve this for my index page, but for category page I'm having problem. To be more specific, I have <a class="link norang" href="/?sort=score&page=1"><b>popularity</b></a> in my template. This works fine. in the same way I have the following code in my category.html <a class="link norang" href="category/{{category.name}}/?sort=score&page=1"><b>popularity</b></a> but this takes me to http://127.0.0.1:8000/category/a/category/?sort=score&page=1 and gives me the error. In my views.py I have this function

def category(request, category_name_url):
    category_name = decode_url(category_name_url)
    hotCat = Category.objects.get_hotCat()
    ad2 = Sponsored.objects.get_ad1()
    try:

                category = Category.objects.get(name=category_name)
                sort = request.GET["sort"].strip()
                sort_method = SortMethods[sort]
                page = request.GET["page"].strip()
    except KeyError:
                sort_method = SortMethods.score
                page = 1

    if sort_method == SortMethods.date:
                thread_list = Post.objects.filter(category=category).order_by("-pub_date")
    else:
                thread_list = Post.objects.filter(category=category)
                thread_list = sorted(thread_list, key=lambda x: x.get_score(), reverse=True)

    paginator = Paginator(thread_list, 30)

    try:
                posts = paginator.page(page)
    except PageNotAnInteger:
                posts = paginator.page(1)
    except EmptyPage:
                posts = paginator.page(paginator.num_pages)
    context = {
                "posts": posts,
                "pages": paginator.page_range,
                "sort": sort_method.name,
                "category":category,
                "following" :following(request.user.id),
                "hotCat":hotCat


        }
    return render(request, "main/category.html", context)

and in the separate file I have

class SortMethods(Enum):

    score = 1
    date = 2

then in html I have

<div id="Space">
  <ul class="shouldwork">
    <li role="presentation" class="sort">
      <a class="link norang" href="/category/{{category.name}}/?sort=score&page=1"><b>popular</b></a>
    </li>
    <li role="presentation" class="date">
      <a class="link updated" href="/category/{{category.name}}/?sort=date&page=1"><b>update</b></a>
    </li>
  </ul>
</div>

This is my traceback

Traceback:
File "env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/main/views.py" in category
  123.              category = Category.objects.get(name=category_name)
File "env/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
  127.                 return getattr(self.get_queryset(), name)(*args, **kwargs)
File "env/local/lib/python2.7/site-packages/django/db/models/query.py" in get
  334.                 self.model._meta.object_name

Exception Type: DoesNotExist at /category/a/category/
Exception Value: Category matching query does not exist.

I would highly appreciate any help

Edit: here's my urls.py

urlpatterns = [
        url(r'^$', 'main.views.index', name='index'),
        url(r'^category/(?P<category_name_url>[\w|\W]+)/$', views.category, name='category'),


        url(r'^post/(?P<slug>[\w|\-]+)/$', views.post, name='post'),

        url(r'^post/edit/(?P<slug>[\w|\-]+)/$', PostUpdateView.as_view(), name='post-edit'),
        url(r'^post/delete/(?P<slug>[\w|\-]+)/$', PostDeleteView.as_view(), name='post-delete'),
        ]

标签: python django
1条回答
男人必须洒脱
2楼-- · 2019-09-09 12:48

The reason it is doing this has nothing to do with django, but rather because you have relative links in your template.

If you are already at a link http://localhost/foo, and you place a link such as <a href="bar/zoo">link</a>, then the resulting URL will be http://localhost/foo/bar/zoo, and not http://localhost/bar/zoo.

You need to adjust your template so that you are using absolute links.

查看更多
登录 后发表回答