Positional Argument Errors in Django

Hi everyone,
Can you help me out?
I’m unable to see why this error is happening:

# TypeError at /categories/int:category_id>/posts/2

see_post() missing 1 required positional argument: 'category_id'

urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.list_all_categories, name='categories_list'),
    path('<int:category_id>/posts', views.list_category_posts, name='category_posts'),
    path('int:category_id>/posts/<int:post_id>', views.see_post, name='post_view'),
]

views.py

from django.shortcuts import render, redirect, HttpResponse
from .models import Category, Post
from .forms import CategoryForm, PostForm
import datetime

def list_all_categories(request):
    categories = Category.objects.all() 
    return render(request, 'categories/category_list.html', {'categories': categories})

def list_category_posts(request, category_id):
    specific_category = Category.objects.get(id=category_id)
    posts = specific_category.posts.all()
    return render(request, 'categories/category_posts.html', {'category': specific_category, 'posts': posts})

def see_post(request, category_id, post_id):
    specific_category = Category.objects.get(id=category_id)
    specific_post = specific_category.posts.get(id=post_id)
    return(request, 'posts/post_view.html', {'category': specific_category, 'post': specific_post})

catagory_posts.html

{% extends 'categories/base.html' %}
{% block content %}
<h2> craigslist junior </h2>
<p> {{category.category_name}}</p>
<ul>
{% for post in posts %}
    <div>
        <li><a href="{% url 'cljunior:post_view' category_id=category.id post_id=post.id %}">{{post.title}}</a></li>
    </div>
{% endfor %}
</ul>

<a href=#>new post</a>
{% endblock %}

Dang, ya’ll I think I see the error. I’m missing a carrot for the url path to “post_view”. Is that the error you all found?

Dang, ya’ll I think I see the error. I’m missing a carrot for the url path to “post_view”. Is that the error you all found?

That was exactly what I noticed – the missing opening < in your urls.py file.

Did changing that fix the error?

Yes. That did fix it.

1 Like