Dear pros. Trying to pass slug to urls.py. To display the page with categories at: http://127.0.0.1:8000/category/obo-vsem/ . But it gives an error (what is wrong) How to fix ?:

TypeError at /category/obo-vsem/ get_category_article() got an unexpected keyword argument 'slug' 

Views.py

 def get_category_article(request, category_id): category=get_object_or_404(Category, pk=category_id) posts=Posts.objects.order_by('-pub_date').filter(is_active=True, category=category) return render(request, 'category.html', {'posts': posts}) 

urls.py

 url(r'^category/(?P<slug>[-\w\d]+)/$', get_category_article, name="get_category_article"), 

    1 answer 1

    I don’t really remember how to work with the named arguments, but try this:

     def get_category_article(request, slug): 

    That is, in theory, the argument name is the same as the name specified in the regular expression in the url, must be present in the view.

    • When adding it another error pops up: def get_category_article(request, category_id, slug): category=get_object_or_404(Category, pk=category_id) posts=Posts.objects.order_by('-pub_date').filter(is_active=True, category=category) return render(request, 'category.html', {'posts': posts}) Error: TypeError at /category/obo-vsem/ get_category_article() missing 1 required positional argument: 'category_id' - kelevra
    • This is natural, because category_id also needs to be renamed. - Mr Fix
    • I have no idea how your code works there. I answered about the named argument. I think that if you have problems working with even ordinary identifiers, then you shouldn’t be involved in this matter. - Mr. Fix